Zend_Json_Server and Dojox RPC

If you’re anything like me you happened on this while searching through all of the other tutorials that get you everything EXCEPT the last little bit to get this rolling. Honestly two more lines on most of the things I read would have made this the easiest thing in the world to do. Instead here I am, writing it in case someone else has the same problem I did.

The first thing you’ll notice if you read tutorials about setting up Zend_Json_Server is that the json_rpc.php file gets put all over the place (some people even put the script in the bootstrap). If you’re wondering, the right place to place it is in the public folder (I’m assuming you set up the Zend Framework with their default configuration that has an application folder with the Controllers, Views, and Models a Library folder, etc).

This is what public/api/json_rpc.php should look like:

<?php
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(dirname(__FILE__) . '/../../library'),
    get_include_path(),
)));
 
require_once "Zend/Loader/Autoloader.php";
require_once "MyAppFolder/RPC.php";
 
$loader = Zend_Loader_Autoloader::getInstance();
 
$server = new Zend_Json_Server();
$server->setClass('RPC');
// RPC is my handler class that I included above
 
if('GET'==$_SERVER['REQUEST_METHOD']){
    // Here the target is the path of the $server->handle() which happens
    // to be this exact path
    $server->setTarget('/api/json-rpc.php')
            ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
    $smd = $server->getServiceMap();
 
    // I read articles that said you need to do this to set Dojo Compatibility
    // but it turns out that including the line below actually made it not work
    // so I commented it out below just as an FYI
    // $smd->setDojoCompatible(true);
 
    echo $smd;
    return;
}
$server->handle();
?>

As you can see from above, none of my RPC handling happens in the MVC framework. I avoid all of the overhead by doing this and it’s easier to deal with anyway. The json_rpc.php is in the public folder, and my RPC handling class is in the library folder alongside the Zend folder in a folder I named MyAppFolder.

Another important note. The class that I included and then loaded for the Zend_Json_Server needs to be parameterized in order for it to be able to create the SMD file to send to the client. If you don’t know what an SMD file is, take a look here. It defines the functions you can call remotely and defines what to expect in return.

What I mean by parameterize is:

class RPC 
{
    /**
     * Return a friendly message
     *
     * @param int id
     * @return string
     */
    public function getMessage($id){
        return "You sent: ".$id;
    }
}

The javascript for your front end is pretty easy too. I implemented mine with Dojo (which is awesome, I recommend it above all the other Javascript toolkits), and it looked like this:

dojo.require("dojox.rpc.Service");
 
var myRpc = new dojox.rpc.Service("api/json-rpc.php");
var dojoDeferredObject = myRpc.getMessage(123);
dojoDeferredObject.then(function(result){
     alert("You said: "+result);
});

This gives me a deferred object (has to wait for the server trip) which I can then use to create the alert or do whatever I want to with the result.

For a great article on dealing with deferred objects look here.

For the original information from Zend, look here.

2 Responses to “Zend_Json_Server and Dojox RPC”

  1. billy bob says:

    The JavaScript code won’t work.

    After the line :
    var myRpc = new dojox.rpc.Service(“api/json-rpc.php”);

    i get :
    Uncaught TypeError: Cannot read property ‘Service’ of undefined

Leave a Reply

Spam protection by WP Captcha-Free