views:

228

answers:

1

I just started on CodeIgniter a few hours ago, I ran into some problems. I am trying to call a function which is currently in a controller named 'Admin'. I am trying to access this from a helper.

How would I do this? I have tried almost everything but nothing seems to be working and outputs:

Fatal error: Call to a member function login() on a non-object

+1  A: 

I agree with jimyi. Your helper and library classes should be independent of any particular controller in an application. If you have a "page title" helper, for example, it shouldn't depend on any particular behavior of your admin controller. What if you wanted to use the page helper in another application that didn't have an admin controller? Or what if you make changes to the admin controller down the road, now you have to make sure those changes don't break your helper.

If your helper function needs some particular bit of data that the admin controller has, you could pass it in as a function parameter from the controller. This way the helper is a dependency of the controller as opposed to the controller being a dependency of the helper.

Additionally, that error message means that whatever thing you are calling has not been instantiated properly. So you are probaly doing something like:

$this->load();

from within the helper. However, in the helper,

$this
is undefined (hence the 'non object' error). CodeIgniter provides a way to load models, libraries, and views from outside of a controller. But, as far as I know, it doesn't allow you to do this for controllers. That's fine though, for the reasons stated above. Gotta be careful when you introduce dependencies.

GloryFish