Welcome Guest, Not a member yet? Register   Sign In
XML-RPC as webservice using good MVC
#1

[eluser]kylehase[/eluser]
I'm looking for the "right" way to develop a webservice using good MVC practices. My webservice will be a pretty standard XML-RPC service using the CI XML-RPC libraries but my question is about how to implement this in MVC.

For instance, I read that an RSS feed should be able to utilize all the same controllers as your main site but simply use different view classes. Shouldn't this also be the case for an XML-RPC webservice? All the examples seem to include model and view in one class.
#2

[eluser]gtech[/eluser]
I'm not sure why you would want to load a view when creating an XMLRPC service. I have a XMLRPC server but all the methods are in the same XMLRPC controller and I call different models from the controller. I format the response from the model into an XMLRPC structure, I dont load a view, as the 'output' is an xmlrpc response. This works well for me, as all my XMLRPC code is in one place, however I don't know if this is the 'correct' way.

I have tried to load controllers methods from outside the XMLRPC controller but for some reason It believe its an unknown method, I am unsure if this is a bug or not but will look into over the next couple of days.
#3

[eluser]kylehase[/eluser]
gtech,

Thanks for the response. I suppose you are right that a view is not necessary but I'm just trying to adhere to best practices to make my application maintainable.

Your comment about loading methods was another question I was going to ask but I didn't want to make my first request too long. I noticed that the XML-RPC library uses
Code:
is_callable()
to determine if the method is callable. Because of this, calling an outside method would require that the method being called is loaded before attempting to use it in the
Code:
$this->xmlrpcs->initialize($config);
line. I believe this should work but have yet to try it. It would surely make the code more maintainable as you'd probably be reusing many existing controller methods in both your main application and webservice.
#4

[eluser]gtech[/eluser]
[quote author="linuxamp" date="1196967092"]gtech,

Your comment about loading methods was another question I was going to ask but I didn't want to make my first request too long. I noticed that the XML-RPC library uses
Code:
is_callable()
to determine if the method is callable.
[/quote]
correct it does

[quote author="linuxamp" date="1196967092"]
Because of this, calling an outside method would require that the method being called is loaded before attempting to use it in the
Code:
$this->xmlrpcs->initialize($config);
line. I believe this should work but have yet to try it.
[/quote]
yes it should enable you to load methods in another controller? (needs reseach) but it doesn't, I have tried it (unless i'm missing something). I use the above method to set the config to enable the methods inside the server controller before initialising anyway.

I can kind of see why it doesn't work... If the method is in the same controller as the server controller its fine.. If the method is in another controller it doesnt work, and I am not sure how to fix it yet as when the request is received it only loads up the specific controller (in case of xmlrpc the server controller), and so the other controllers methods are not available.

I have posted up some xmlrpc fixes before so that the system methods would work eg system.listmethods is broken in 1.5.4. have a look in the bug log forum.

I am nowhere near being a CI expert however so don't know if not being able to load another controllers methods its a bug or not yet, i need to sit down and look at it properly (if it is I will log it unless you want to).






Quote: It would surely make the code more maintainable as you'd probably be reusing many existing controller methods in both your main application and webservice.

Not really, my xmlrpc code is used to make changes to the model directly, the controller functionality is different, a controller may load several models for one page.

My xmlrpc is used so other web guis can interact with my model schema, so its purley an interface into calling models. I don't see the point of an xmlrpc request calling a 'standard' controller method that loads a view, as the xmlrpc response will be meaningless, to make it work you would need to create a view and a xmlrpc response for every controller method you call.




P.S all the initialise function really does is set up an array $this->methods so the _execute function can determine if the method is expected or not.. it doesn't source in other controllers methods.

Code:
.....    
function initialize($config=array())
{    
    if (isset($config['functions']) && is_array($config['functions']))
    {
        $this->methods = $config['functions'];
    }
.....


function _execute($m)
{
    $methName = $m->method_name;
        
    // Check to see if it is a system call
    // If so, load the system_methods
    $sysCall = ereg("^system\.", $methName);
    $methods = $sysCall ? $this->system_methods : $this->methods;


I hope this helps, I might of waffled on too much, im sure my approach is not the only approach though.




Theme © iAndrew 2016 - Forum software by © MyBB