CodeIgniter Forums
Newbie question: How do i reference another controller from a controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Newbie question: How do i reference another controller from a controller? (/showthread.php?tid=27697)



Newbie question: How do i reference another controller from a controller? - El Forum - 02-17-2010

[eluser]Unknown[/eluser]
So here is what i want to do. I want to restrict parts of my site to only users who are logged in. Lets say I have a 'User' controller; how can I call that from another controller to see if User->isLoggedIn();?

Basically a way to reference controllers from one another? is that a bad practice?

Thanks


Newbie question: How do i reference another controller from a controller? - El Forum - 02-17-2010

[eluser]kgill[/eluser]
Controllers don't call other controllers so yes that's bad practice but from your description you sound like you're describing models not controllers. Something like User->isLoggedIn() would be in your model and you'd call that from any controller you want whereas the controller is accessed via the URL:

mysite.com/controller_name/method_name/params

If you haven't read the user guide yet I strongly recommend you do so.


Newbie question: How do i reference another controller from a controller? - El Forum - 02-17-2010

[eluser]Unknown[/eluser]
but in this case;the 'isLoggedIn()' method would be just checking a session variable.. You can imagine me having another function such as 'getUserUnreadMsg(); would those go into user model as well?

so Model is anything that queries the DB?


Newbie question: How do i reference another controller from a controller? - El Forum - 02-17-2010

[eluser]dedenf[/eluser]
you can use a helper for that.
FYI, a helper can call libraries, models (pretty much resource that ci loads).


Newbie question: How do i reference another controller from a controller? - El Forum - 02-17-2010

[eluser]bretticus[/eluser]
@kgill is right. You wouldn't call a controller method from a different controller. A controller is pretty much only for being an entry point into your application (it controls what happens when a specific URI is employed.)

To accomplish what you are trying to do is most commonly performed through a library. There are several authentication libraries that you can just download for CodeIgniter. Or, of course, you can roll your own.

I understand why a users controller seems like a good fit with authentication. But really, you'll do better to separate what you can do to a user from the necessary authentication required to do so.