CodeIgniter Forums
Use one method in multiple controllers ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Use one method in multiple controllers ? (/thread-28974.html)



Use one method in multiple controllers ? - El Forum - 03-26-2010

[eluser]Monarobase[/eluser]
Hello, I'm sorry if my title is a bit confusing, I will try and explain myself better with more words than can hold in the title.

I have just finished developing an application that runs with a single controller.

My customer has now asked me to program a second application and to have a central log in system and has told me that there will be more applications to come.

I would like to build a system so it is easy to plug in another application just by uploading the file(s) of the second application any maybe if necessary adding the application info to the database (application image, name etc ...).

At the moment I have a login interface that shows the login form on any page (method) that requires the user to be logged in without redirecting to another page so that when the user loggs in, he gets the correct page straight away.

To achieve this I use the following code :

Code:
if(!$this->session->userdata('logged_in')) {
            $this->login();
} else  {
//Method code goes here
}

The login method calls a login page view which contains a form that points to the current open URI so that when the user logs in he stays on the same page he requested...

The login function uses a library to interact with the database.

Now I'm looking for the best way to make my code "plugable", in other words I want a user to be presented with a login page and when he logs in to be presented with a list of all the available applications and each application to be contained in a separate file.

My idea is to have a login controller and then to add on different controllers so I would have for example :

login.php
addressbook.php
diary.php
photogallery.php

But if I did this, I would then need to be able to call the login page from all of my controllers or duplicate the login functions in both controllers.

I would like the URL's to be http://mydomain.com/addressbook for the address book http://mydomain.com/diary for the diary and http://mydomain.com/photogallery for the gallery

And I would like a user to be able to log into http://mydomain.com and be presented with a list with a link to each application.

So I guess my question is how is the best way to make one method including calls to views to be accessible from different controllers ?

The reason why I would like this is to have, for a user that is not logged in to be presented with the same page (the login page) on all four of these URL's without redirecting him :

http://mydomain.com/home
http://mydomain.com/addressbook
http://mydomain.com/diary
http://mydomain.com/photogallery

But once he logs in he must see the appropriate page :

The /home would show the list of applications
The /addressbook would show the index of addressbook
The /diary would show the index of diary
The /photogallery would show the index of the photogallery.

Another important thing to take into account, I already have the addressbook inside a controller called addressbook.php and when you log into this controller it show the index of /addressbook and I would like to have very few changes to make to it.

I hope I have been clear enough and not to much of a pain with the length of my explanations.

Thanks in advance !


Use one method in multiple controllers ? - El Forum - 03-26-2010

[eluser]Monarobase[/eluser]
A simple require_once on a file that contains just the method would do the job but would somewhat break the whole principle of codeigniter and I'm sure it's not the best way.

I have looked into the different ways I could use to call this method but normally helpers and libraries and don't call views...

I'm seriously thinking about duplicating my login method in all controllers unless someone can suggest a better way around this.


Use one method in multiple controllers ? - El Forum - 03-26-2010

[eluser]danmontgomery[/eluser]
You probably want to extend the controller class (MY_Controller), put the method in there, and have all of your controllers extend that class.


Use one method in multiple controllers ? - El Forum - 03-27-2010

[eluser]Monarobase[/eluser]
Thanks that's just what I needed. Works like a charm !


Use one method in multiple controllers ? - El Forum - 05-02-2010

[eluser]SaminOz[/eluser]
I had a similar problem (I'm new to CI). My solution was to use redirect(controller/method). Is that bad?