Share » Learn » eZ Publish » ezjscore: eZ Publish JavaScript and...

ezjscore: eZ Publish JavaScript and Ajax framework

Wednesday 23 December 2009 10:45:21 am

  • Currently 4 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Creating Ajax server calls

To create Ajax server-side calls, you will need a PHP class and the appropriate ezjscore.ini settings.

Server call class

The following is some boilerplate code to create a server-side Ajax function. All the function does is return "Hello" appended by the name you supply it. Assuming the extension name "ezjscore_demo", place your server call class in extension/ezjscore_demo/classes/ezjscoredemoservercallfunctions.php with the following content:

<?php
/**
 * File containing the ezjscoreDemoServerCallFunctions class.
 *
 * @package ezjscore_demo
 * @version //autogentag//
 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */

class ezjscoreDemoServerCallFunctions extends ezjscServerFunctions
{
    public static function search( $args )
    {
        if ( isset( $args[0] ) )
        {
            return 'Hello World, you sent me 
                    parameter : ' . $args[0];
        }
        else
        {
            $http = eZHTTPTool::instance();
            if ( $http->hasPostVariable( 'arg1' ) )
            {
                return 'Hello World, you sent 
                        me post : ' . $http->postVariable( 'arg1' );
            }
        }

        return "Request to server completed, 
                but you did not send any 
                post / function parameters!";
    }
}
?>

The functions in this class will be reachable as configured in ezjscore.ini, as explained next.

Setting up ezjscore.ini

To be able to reach the example function, you must first declare the function and make it available to the permission system. The file extension/ezjscore_demo/settings/ezjscore.ini.append.php contains:

[ezjscServer] FunctionList[]=ezjscdemo

It also contains the following settings block. It maps the server call "ezjscdemo" to the PHP class "ezjscoreDemoServerCallFunctions" and specifies the file in which the class is located. To be able to test permissions, we also have “Functions[]=ezjscdemo”.

[ezjscServer_ezjscdemo]
Class=ezjscoreDemoServerCallFunctions
# Define File to avoid relying on autoload 
# system for this simple example
File=extension/ezjscore_demo/classes/ezjscoredemoservercallfunctions.php
Functions[]=ezjscdemo

Testing

You can do a simple test that the server-side function is accessible by loading the following URLs:

http://<root>/ezjscore/call/ezjscdemo::search
http://<root>/ezjscore/call/ezjscdemo::search::Strawberry Fields

Keep in mind that this test is basic. Depending on the characters for the supplied name and the browser you use, putting the name in the URL might not work. It is best to use post parameters, as shown in extension/ezjscore_demo/classes/ezjscoredemoservercallfunctions.php and the examples in the "Bundled JavaScript libraries" section above or the example template code in the ezjscore_demo extension.

If you run the same simple test from above when you are an anonymous user, you should get the response “Not a valid ezjscServerRouter argument: test::name”. The relevant user(s) should be given access to ezjscore/call/ezjscdemo via the regular eZ Publish permission system.

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)