Welcome Guest, Not a member yet? Register   Sign In
Newbie question: How do i reference another controller from a controller?

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?


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:


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

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?

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

@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.

Theme © iAndrew 2016 - Forum software by © MyBB