Part of the EllisLab Network
   
1 of 2
1
How to call functions of another controller
Posted: 27 June 2007 09:33 PM   [ Ignore ]  
Summer Student
Total Posts:  15
Joined  06-25-2007

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!

Profile
 
 
Posted: 27 June 2007 11:29 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  314
Joined  12-26-2006

Hi YongKhun,

The way I approach this is to move all common functions to be called into a Helper that I have created in the helper directory then use the following:
 
 
helpers/helper_with_common_functions.[hp

function common_function(optional_parameter='')
     
$result = 'result from common_function()';

     return
$result;
   
}


 
 
controller_another.php

$this->load->helper('helper_with_common_functions' );
   
$data['result_from_common_function'] = $this->common_function();


 
 
controller_different.php

$this->load->helper('helper_with_common_functions' );
   
$data['result_from_common_function'] = $this->common_function();


 
 
Check out the user manual on Helpers, LIbraries and Models.
 
Cheers,
 
John_Betong

 Signature 

Real life ...  (development site)
My Hippy Trail    Source code   
Latest Project

Profile
 
 
Posted: 27 June 2007 11:50 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  165
Joined  08-22-2006
yongkhun - 27 June 2007 09:33 PM

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.

 Signature 

Don’t argue with an idiot, people watching may not be able to tell the difference.


marcoss
http://defmay.com
http://fenix.st

Profile
 
 
Posted: 28 June 2007 12:26 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  747
Joined  02-05-2007
marcoss - 27 June 2007 11:50 PM

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.

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 28 June 2007 01:11 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  165
Joined  08-22-2006
Rick Jolly - 28 June 2007 12:26 AM

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.

 Signature 

Don’t argue with an idiot, people watching may not be able to tell the difference.


marcoss
http://defmay.com
http://fenix.st

Profile
 
 
Posted: 28 June 2007 04:37 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  55
Joined  08-23-2006

This topic should be a ‘sticky’ - it comes up so many times! hmmm

 Signature 

Senior Developer, MyBuilder.com

Hierarchical data trees using nested sets

Profile
 
 
Posted: 28 June 2007 09:09 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

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.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 28 June 2007 09:14 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  55
Joined  08-23-2006

Aye, you’re right there linuxbz. A single “common questions / FAQs” sticky would be far better than multiple stickies!

smile

 Signature 

Senior Developer, MyBuilder.com

Hierarchical data trees using nested sets

Profile
 
 
Posted: 28 June 2007 08:35 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  69
Joined  05-20-2006

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.

Myles

 Signature 

Myles Wakeham
Director of Engineering
Tech Solutions USA, Inc.
Podcast Hosting at CyberEars.com
Follow me on Twitter!

Profile
 
 
Posted: 28 June 2007 11:00 PM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1321
Joined  07-27-2006

Uh.. butlers?

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.

 Signature 

Check out the Template Library

Profile
 
 
Posted: 29 June 2007 04:39 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  55
Joined  08-23-2006
Colin Williams - 28 June 2007 11:00 PM

Uh.. butlers?

Actually, I quite like that analogy (although the stuff that came after it was a bit too fluffy for my head! wink  ). 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… wink )

 Signature 

Senior Developer, MyBuilder.com

Hierarchical data trees using nested sets

Profile
 
 
Posted: 29 June 2007 05:05 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
RankRankRank
Total Posts:  915
Joined  07-10-2006

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.

Profile
 
 
Posted: 09 July 2007 02:15 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  15
Joined  06-25-2007

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

any advice? Thanks.

Profile
 
 
Posted: 09 July 2007 02:54 AM   [ Ignore ]   [ # 13 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1321
Joined  07-27-2006

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.

 Signature 

Check out the Template Library

Profile
 
 
Posted: 09 July 2007 03:09 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  15
Joined  06-25-2007

Thanks Colin, for your fast advice.

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?

Profile
 
 
Posted: 09 July 2007 03:25 AM   [ Ignore ]   [ # 15 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1321
Joined  07-27-2006

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.

How about a config file?

 Signature 

Check out the Template Library

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64457 Total Logged-in Users: 22
Total Topics: 80969 Total Anonymous Users: 1
Total Replies: 435702 Total Guests: 171
Total Posts: 516671    
Members ( View Memberlist )