Is there a way to call the functions of another controller from a different controller? I try to do like $this->another_controller->function1() but error said “calling function on non-object”. How? Thanks!
Is there a way to call the functions of another controller from a different controller? I try to do like $this->another_controller->function1() but error said “calling function on non-object”. How? Thanks!
No. And that is not the way to go with MVC. You should use a Model to place the reusable functions and the call them in the Controllers that will output the data using a View.
You may use the helpers approach as well, but only if it is just one small function, like an string replace or something like that.
No. And that is not the way to go with MVC. You should use a Model to place the reusable functions and the call them in the Controllers that will output the data using a View.
You may use the helpers approach as well, but only if it is just one small function, like an string replace or something like that.
Technically, a Model is an interface to data. So although you could use a model for common functions, it is more correct to use a helper or a library. Also, I don’t see any reason why you couldn’t use a helper for many large functions.
Technically, a Model is an interface to data. So although you could use a model for common functions, it is more correct to use a helper or a library. Also, I don’t see any reason why you couldn’t use a helper for many large functions.
Maybe i miss-expressed myself, i was trying to say complex or advanced functions, which usually have to deal with data sources or are going to be reused along the controllers, in those cases is where you would use Models, Libraries and such.
I really don’t like stickies ... those of us who frequent the forums have to scroll past stickies every time we visit the forum. I think things like that should be in the wiki, or incorporated into the user guide, or even placed in a FAQ.
If we HAVE to have stickies, make it ONE sticky that has links to all the stuff a newbie ought to look at.
There definitely seems to be a lot of confusion with noobs regarding the philosophy of MVC. Its almost like there needs to be an exam first so that people understand the role of seperation of UI, transforms and data. But alas….
Controllers that have functions in them that are used repetitively, where the function isn’t a simple call from a UI element (ie. HTML page) back to the application are typically poorly designed and definitely not ‘MVC friendly’. What I do (and forgive me if the syntax has changed but I’m still using 1.4.1 CI), is to use PlugIns as a repository for all of my function libraries and make sure the controllers just load and have access to the necessary plugins in their creation. I normally force the web page to act as the ‘contract party’ with the controller, so if a web page requests something, the controller is its point of communication. Like a butler at a restaraunt. And like a restaraunt, I can’t (as a customer) expect to go into the kitchen and interact with the cooks. My ‘contract relationship’ is with the butler.
The ‘butler’ is indicative of the controller. If the controller needs to send me to another table, then they place the burden back onto me to interface with a different controller (ie. move). Therefore the controller never needs to communicate with another controller. They just all share a common function repository, like all butlers share access to the kitchen to order and get meals as they are prepared.
In this analogy, do butlers ever communicate with other butlers? Sometimes, but it should be rare. The controller sends back the redirection to the web page in order to send them somewhere else to do something. As the controller should be primarily a UI interface, then it means that its contract relationship should only be to a UI.
I think the best approach here is to read through the user guide and decide what is most appropriate to your task at hand.
Usually, if I have a set of functions that I’m going to use across many controller, and maybe even models, that not only interacts with a database, but also provides helper functions, I go the library route. Also, if it’s something I see myself using in another app, like user authentication, I’ll abstract it into a library.
Actually, I quite like that analogy (although the stuff that came after it was a bit too fluffy for my head! ). I may use it again!
If the restaurant is the application, then the customer is the browser, the waiter is the controller and the kitchen is the model plus libraries plus helpers etc (ie the core of the application).
The customer doesn’t go and make his own food, he requests a dish from the waiter.
The waiter (controller) doesn’t make the food, he goes to ask the kitchen (model etc) for it. He might take additional instructions with him (hold the mushrooms, easy on the relish etc (analogous to form data and/or url parameters)) but he doesn’t get involved in the preparation.
The kitchen then prepares the food, which the waiter then serves to the customer.
The waiter’s job is to take the requests and serve the results.
(And occaisionally try to catch the guy near the window who thinks he can make a break for it without paying… )
Creating a library based on the Factory pattern is also a good solution. The factory library can be loaded once by a base controller and its get functions can delegate to other libraries or helpers without using includes. Library or helper loading and calls to the associated library or helper functions can be made in the factory get functions. Then you effectively load the factory library with minimum memory overhead and call get functions as needed within your controllers, and the get functions delegate to the libraries when called. I think this is more in tune with the MVC approach and a good compliment to CI’s use of the lazy-initialization pattern to load only what is necessary.
Now I can load another controller and call its function using the following lines to load the controller:
include_once(APPPATH.'controllers/external.php'); $ext_class = new External();
But now another problem arises: the loaded external controller seems like not loaded properly although all its functions can be called. It seems like within the external controller, the library object are not loaded properly. I got this error if I called the controller from another controller:
Fatal error: Call to a member function on a non-object
If the controller function is called from browser as in normal case, no problem at all….
My advice is to change your application’s logic and move any functions that are required by two controllers into either a model, library, or helper. Work with CI and not against it.
I did consider to put the function as library. But I found that my function will be more efficient to remain in the external controller due to some class member variables. If I put it as library function, then I will have to perform some extra initialization of variables.
So, there is really no proper way of calling function in another controller?
I can’t imagine a problem right away. I would to further investigations into the errors your receiving. Perhaps the controller being included is trying to call library methods that do not exist.
If I put it as library function, then I will have to perform some extra initialization of variables.