• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call functions of another controller

#1
[eluser]yongkhun[/eluser]
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!

#2
[eluser]John_Betong[/eluser]
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
Code:
function common_function(optional_parameter='')
     $result = 'result from common_function()';

     return $result;
   }
 
 
controller_another.php
Code:
$this->load->helper('helper_with_common_functions' );
   $data['result_from_common_function'] = $this->common_function();
 
 
controller_different.php
Code:
$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

#3
[eluser]marcoss[/eluser]
[quote author="yongkhun" date="1183016028"]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![/quote]

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.

#4
[eluser]Rick Jolly[/eluser]
[quote author="marcoss" date="1183024219"]
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.[/quote]

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.

#5
[eluser]marcoss[/eluser]
[quote author="Rick Jolly" date="1183026392"]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.[/quote]

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.

#6
[eluser]thunder uk[/eluser]
This topic should be a 'sticky' - it comes up so many times! Undecided

#7
[eluser]linuxbz[/eluser]
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.

#8
[eluser]thunder uk[/eluser]
Aye, you're right there linuxbz. A single "common questions / FAQs" sticky would be far better than multiple stickies!

Smile

#9
[eluser]Myles Wakeham[/eluser]
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

#10
[eluser]Colin Williams[/eluser]
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.