Welcome Guest, Not a member yet? Register   Sign In
Controller calls another controller
#1

[eluser]gao[/eluser]
Hello. I'd like to know how a controller could call another controller. I've read the library solution but it does not fulfill the Model-View-Controller pattern. The logical is that if I have, for example, a dispatcher controller that controller could delegate the action to another controller.

For example. I have a controller ClientsController. This controller has a method called list(). I want to use that method to show a webpage listing the clients but I also want use it in a xml-rpc call. How? I think that the correct way is:

XmlRpcController -> ClientsController -> list();

I mean, the request arrives to the XmlRpcController, this one calls the list method in the correct controller...

Regards.
#2

[eluser]Colin Williams[/eluser]
Many topics have covered this on this forum. Have your searches come up empty?

My standing is that the Router class is the dispatcher, not a controller. And there are better ways to share common code among controllers (libraries, extend the base controller, models, etc.)
#3

[eluser]gao[/eluser]
First of all thanks for your reply.

I've already searched for an elegant solution on the forum and, since I wrote a new thread, it means I couldn find any "convincing" solution.

First of all, I cannot use a Router class, we are talking about XMLRPC comunication and I would like to have an XMLRPC main controller which dispatches all the xmlrpc requests calling different contoller methods and building a valid response.

Let's say for example I'd like to access to some listing functions for different entities, each entitiy has his listAll() method in his controller. (there's a webpage beneath XMLRPC comunication which actually uses that methods).

If I received an XMLRPC call for a procedure which collects all the entities (joining, for example, listing function result within different controllers) I'd have to call X different controller methods to collect them all bypassing my main XMLRPC controller. (routing every request to a specific controller method call).

I would like to have an XMLRPC main controller which builds a valid response calling specific functions in different controllers and, IMHO, I find absolutely meaningless to extract all the controller "shared methods"
to a different place such a library or an helper.

With a good OOP understanding I can use the same list() method within a controller both from the controller itself and from an XMLRPC controller somewhere else without redundancy code or illogical code separation.

That's what I mean for good OOP architecture :

"In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility." (Single responsibility principle)

I'm not english native speaker, so i apologize for any error.
Thanks a lot.
#4

[eluser]Colin Williams[/eluser]
Why would you not have one controller to serve both an HTML page and an XML-RPC call?

Your single responsibility with a controller is to serve requests, period. And typically you want to separate your controllers by resource, not request type. Later on you might serve JSON. You don't want to create a separate JSON controller. These are just different response formats.
#5

[eluser]wiredesignz[/eluser]
Hi gao, Modular Extensions HMVC will allow you to call controllers from other controllers. (see my signature for linkage)

I would be very interested to know if you find this a "convincing" solution or not.
#6

[eluser]gao[/eluser]
Hello wiredesignz. I've gave it a try but I came up with some errors.
I followed every step but I couldn't do it works.

Colin Williams, first of all it seams I cannot mix methods which are going to be called by a web request and an XML-RPC one in the same controller.
The XML-RPC system disallows methods which are not included in the xmlrpc config array since I launch the ->serve() method.

In OOP everything must be independient. Let's think about 50 controllers. If I put the XML-RPC system in each one and in the future I wanted to remove that specific service one day , having a controller which calls directly another controller, I would only have to delete one file.

With your approach it looks I would have to delete redundant code in every controller.
That means 0% of maintenance efficience to my eyes.I'm looking for an high cohesion solution in order to reuse my code.

I'm sorry but I think your "external library" approach just does not fit a good OOP architecture.
I cannot call any other method from within a static context, I mean, if I used codeigniter separate libraries I would have some business logic inside the library and some other in the controller itself.

Hope u understand what I'm trying to describe.

Thanks in advance for your reply.
#7

[eluser]Colin Williams[/eluser]
I see nothing standing in your way of having a controller that serves both requests. I think your statement about "method prevention" is a misinterpretation of the XMLRPC class with CI.

However, if you insist on separating out the controllers, wiredesignz Modular Extensions setup is the best way to go.




Theme © iAndrew 2016 - Forum software by © MyBB