Topic: Codeigniter with Xajax0.7 and Smarty

I have been tryingto get a hold on the new Xajax by Thierry FeuZeu and I feel pretty good about it.

moo has been trying to integrate the new Xajax into CI and I would like to show my implementation...


<?php defined('BASEPATH') OR exit('No direct script access allowed');
    Author: Ed Robinson
    Demonstration of CodeIgniter MY_Controller extending the CI_Controller
    and adding xajax 0.7 alpha and Smarty templating engine.
    The order of things is important.
    Pay particular attention to the dispatch() method. It
    does away with xajax method registration.
require (__DIR__ . '/../third_party/xajax/vendor/autoload.php');
require '/../third_party/smarty/libs/Smarty.class.php';

use Xajax\Xajax;
use Xajax\Response\Response;
use Xajax\Request\Factory as xr;

class MY_Controller extends CI_Controller{
    public $xajax; //Xajax instance
    public $resp;  //Response instance    
    public $smarty; //Templating engine
    public function __construct()
        //The following order is important
    private function setupXajax()
        $this->xajax = Xajax::getInstance();
        $this->xajax->setOption('core.prefix.function', 'xajax_');
        $this->resp = new Response();


    //Setup the templating engine
    private function setupSmarty()
        //Instance Smarty
        $this->smarty = new Smarty;
        //Set the Smarty directories.
        //Assign the xajax client side javascript for this page
        $this->smarty->assign('xajaxjsinclude', $this->xajax->getJsInclude());
            Function: dispatch
                $method_name - name of the method to call
                $data - optional arguments to pass to the function
            Using this method you only have to register a single
            function for any xajax'ed class method.
            The method checks if the method exists in the object
            and if it is callable. If so, it is called passing the data
            to it.
            If not - not a method or is not public or protected - an error alert
            is issued.
            There may be more to do security wise.
    function dispatch($method_name, $data='')
        //Call the method iif it can be called
        if(method_exists($this,$method_name) && is_callable(array($this,$method_name)))
            return $this->$method_name($data);
            $this->resp->alert("Error: $method_name is not a valid or not a callable method");
            return $this->resp;


Note the dispatch function. It really cleans things up.

MY_Controller test:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
    Test MY_Controller

class MC_Test extends MY_Controller
        At page load index is called.
        We only load our view in this example...
        Use index to do any page setup you need.
        You can also add Smarty assigns here before displaying
        your template.
        See the hello_world.tpl template file.
    public function index()
        $this->smarty->assign('pagetitle','Hello World');
        This is a callable method that
        is available through MY_Controller's
        dispatch method.
    public function sayHello($name='')
        $this->resp->alert("Hello $name...");
        return $this->resp;

Test controller Smarty template:

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <input type="button" value="Click" onclick="xajax_dispatch('sayHello','Joe')" />

{* Place the Xajax javascrpt *}

By extending the CI_Controller you have a quick and easy gateway to CI with Xajax and Smarty.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Codeigniter with Xajax0.7 and Smarty


I've created a skeleton for the CodeIgniter package on Github:
The package includes code from the my Laravel package an from the examples published here.

Now there are some decisions we need to take before going any further:

1. The location and a namespace of the Xajax classes.
Once this is set, the framework will be able to automatically parse the specified directory and register all the Xajax classes it contains. The addClassDir() and registerClasses() methods were added to the Xajax class to ease the implementation of this feature.
I would suggest the application/xajax as default directory, and \Xajax\CI as namespace.

2. The location and the format of the config file.
The config file shall provide options both for the Xajax library and the user application. The user application options include the Xajax path and namespace defined above.

As the CI Xajax library automatically reads the config file, and sets the specified options, the Xajax_Controller provided in the package works as is, without any change.