Welcome Guest, Not a member yet? Register   Sign In
how to call a controller's method from an other one?
#1

[eluser]ppeemm[/eluser]
Hi there

I am in a controller and I would like to call methods of another controller.
how can I do that?

thanks in advance
#2

[eluser]Michael Wales[/eluser]
Place the methods in a library or model and call them in that manner, from both of your Controllers. Controllers are not designed to talk to one another (it can be done, but it's against the MVC Gods wishes).
#3

[eluser]ppeemm[/eluser]
ok will try this way
cheers
#4

[eluser]esra[/eluser]
[quote author="walesmd" date="1189796961"]Place the methods in a library or model and call them in that manner, from both of your Controllers. Controllers are not designed to talk to one another (it can be done, but it's against the MVC Gods wishes).[/quote]

Maybe for the original versions of MVC 2 but not MVC or even current versions of MVC 2 implemented elsewhere. The MVC pattern applies to separation of controller, business logic and presentation code. If the MVC 2 design pattern gurus had some of the functionality currently available on the web when MVC 2 was developed, MVC 2 might have been very different. Generally, most MVC frameworks for PHP are based on the ideas of their developers and tend to vary considerably from framework to framework. The CI framework user_guide doesn't provide documentation or workaround solutions but plenty of solutions exist on the forums.

Aside from this, other patterns have been developed and are used in many MVC frameworks. For example, the observer and observable patterns could be used. These patterns allow a controller to trigger an event which when observed executes some action. That action could be the execution of a method in another controller, a call to a factory pattern method, and any number of other actions.

The current version of the Modular Separation extensions for CodeIgniter include new Loader methods for accessing methods in another controller. You might do a search for 'Zacharias Modular Separation' and try playing with that approach. It could be adapted to work with the stock CI implementation by extending Loader overloading a few methods and adding additional methods. You would basically have to change the directory search paths. Modular Separation gives CI a different file system which includes a modules directory where the controllers, models, and views of a module are stored in their own directory structures for portability, installation, de-installation, and extensibility reasons. The most current version allows other CI extensions to be stored with a module including language files, module specific helpers and libraries, a module-specific config file, etc.




Theme © iAndrew 2016 - Forum software by © MyBB