1 (edited by thierry.feuzeu 2016-03-26 7:12:37 PM)

Topic: The new Xajax is out, and the new Xajax is cool!

I started working on a recode of the Xajax library a few weeks ago, and today, I am pleased to present you with the new Xajax library.
The main goal was to use the latest PHP features to make the Xajax library extensible and modular.

So what is it about?

The new Xajax is modular and extensible

The library is now separated into two main packages.

The first package is the core PHP library https://github.com/lagdo/xajax-core. It provides the main Xajax class, which is now named Xajax, and implements the functionalities needed to export PHP classes to javascript, and handle the Xajax requests and responses. All the classes in this package are in the Xajax namespace.

The second package contains the javascript files of the library https://github.com/lagdo/xajax-js. Having a javascript only package will later allow to use an open source CDN like https://cdnjs.com or https://www.jsdelivr.com to deliver the files. By default, the Xajax core library gets the files from a server of mine, which offers HTTP and HTTPS access (thanks to Let's Encrypt). Of course, they can also be installed on a private server.

This two packages are the minimum requirement to create an Xajax application, however the library can be extended with additional plugins.
I have created three plugins, which are used in the sample codes in the xajax-examples package https://github.com/lagdo/xajax-examples.

https://github.com/lagdo/xajax-toastr: this package implements javascript notifications with the Toastr JQuery plugin https://github.com/CodeSeven/toastr.
https://github.com/lagdo/xajax-pgwjs: this package implements responsive modal dialog with the PgwModal library http://pgwjs.com/pgwmodal.
https://github.com/lagdo/xajax-bootstrap: this package implements modal dialog with the Twitter Bootstrap library. It requires to manually install the Twitter Bootstrap javascript and CSS files to work properly.

What's new is this package

First of all, the new Xajax library is namespaced, uses PSR-4 autoloading, and installs with Composer.
Some features are deprecated, some others, like javascript code generation (see the examples below) are recoded, and some new features are added.

The new feature I consider the most important is the ability to automatically register all the classes in one or more directories, each directory being associated to a namespace or not.
When exporting PHP classes from a directory, the generated javascript classes are named accordingly. In a directory D, the class A in the file D/S/A.php will generate the javascript class S.A, and if the associated namespace is N, the generated javascript class will be N.S.A. As we'll see in the examples below, the new Xajax library also takes advantage of PHP autoloading to optimize request processing.
This feature makes the Xajax library more suitable for use in modular applications with hundreds of classes.

Examples

Samples code demonstrating the use of the new Xajax library are provided in the xajax-examples package: https://github.com/lagdo/xajax-examples. All examples are based on the helloword.php example in the original Xajax repository https://github.com/Xajax/Xajax/blob/mas … oworld.php. The installation process is described in the homepage of the repository. After the examples are installed, enter the name of any file in the browser to see the result.

- hello.php
This example shows how to export a function with Xajax.

- class.php
This example shows how to export a class with Xajax.

- merge.php
This example shows how to export the generated javascript code in an external file, which is then loaded into the webpage.
You'll need to create the "deferred" directory, and adapt the parameters of the call mergeJavascript() function to your webserver configuration for this example to work.
The compression of the generated javascript code is not yet implemented.

- plugins.php
The example shows the use of Xajax plugins, by adding javascript notifications and modal windows to the class.php example with the xajax-toastr, xajax-pgwjs and xajax-bootstrap packages.
Using an Xajax plugin is very simple. After a plugin is installed with Composer, its automatically registers into the Xajax core library. It can then be accessed both in the Xajax main object, for configuration, and in the Xajax response object, to provide additional functionalities to the application.

- classdirs.php
This example shows how to automatically register all the PHP classes in a set of directories.
The classes in this example are not namespaced, thus they all need to have different names, even if they are in different subdirs.

- namespaces.php
This example shows how to automatically register all the classes in a set of directories with namespaces.
The namespace is prepended to the generated javascript class names, and PHP classes in different subdirs can have the same name.

- autoload-default.php
This example shows how to optimize Xajax requests processing with autoloading.
In all the examples above, an instance of all exported Xajax classes is created when processing a request, while only an instance of the requested class is needed. In an application with a high number of classes, this can cause performance issues.
In this example, the Xajax classes are not registered when processing a request. However, the Xajax library is smart enough to detect that the required class is missing, and load only the necessary file.

- autoload-composer.php
This example illustrates the use of the Composer autoloader.
By default, the Xajax library implements a simple autoloading mechanism by require_once'ing the corresponding PHP file for each missing class. When provided with the Composer autoloader, the Xajax library registers all directories with a namespace into the PSR-4 autoloader, and it registers all the classes in directories with no namespace into the classmap autoloader.

- autoload-disabled.php
In this example the autoloading is disabled in the Xajax library.
The developer then needs to provides its own autoloading, otherwise the Xajax library will raise an error at any attempt to register classes from a directory.
This example will not work until the autoloading is setup.

What's next?

This version of the package is still an early alpha release, not yet suitable for use in production.
There is still much work to do, and in my point of view the most important ones are:
- Check security, mainly for input data.
- Write tests.
- Write documentation and tutorials.

All ideas, all comments and any help are welcome.
The plugin interface is quite stable now, so anyone willing to write a plugin is encouraged to do so.

Thanks,
Thierry

Re: The new Xajax is out, and the new Xajax is cool!

I have made some changes to the xajax-examples package.

A third-party autoloader is now used in the autoload-disabled.php example, so it does not need to be modified anymore to work.
There are sample PHP codes in the example pages, to show how they are implemented.

I've installed the xajax-examples package on a demo server online http://xajax.lagdo-software.net.

Re: The new Xajax is out, and the new Xajax is cool!

Thank you SO much! I can't wait to try it out!!!

Re: The new Xajax is out, and the new Xajax is cool!

Thank you so Much! Greetings from Peru!

Re: The new Xajax is out, and the new Xajax is cool!

Hi,

The new Xajax library has reached what I consider an important milestone. All the features I planned in the core package are implemented.
I'm going to focus now on testing and documenting the library, making a few more plugins, and integrating to a few frameworks.

As the first stable release is getting closer, I'm now thinking of moving to the version number to 1.0.0, since there has been so many important changes on the library.

Re: The new Xajax is out, and the new Xajax is cool!

adiev86 wrote:

Thank you so Much! Greetings from Peru!

You are welcome!!

Re: The new Xajax is out, and the new Xajax is cool!

thierry.feuzeu wrote:

Hi,

The new Xajax library has reached what I consider an important milestone. All the features I planned in the core package are implemented.
I'm going to focus now on testing and documenting the library, making a few more plugins, and integrating to a few frameworks.

As the first stable release is getting closer, I'm now thinking of moving to the version number to 1.0.0, since there has been so many important changes on the library.


This is great news Thierry! Are you planning for a CodeIgniter implementation as one of your target frameworks?

Best regards from Memphis, Tennessee in the USA!  :-)

Jason

8 (edited by moo 2016-04-26 7:26:54 AM)

Re: The new Xajax is out, and the new Xajax is cool!

Hello All,

I'm trying to write a library for the newly resurrected Xajax project (from Thierry Feuzeu)...

and I'm coming up short due to my inexperience in writing CodeIgniter Libraries and with PHP namespacing.

Could anyone offer some thoughts on how to make this work? (see's Thierry's example code: https://github.com/lagdo/xajax-examples … /hello.php)

Here's what I've got so far, and it definitely instantiates an xajax object, of which I can access at least the getVersion() method from inside a CodeIgniter controller method, but I can't figure out how to write the Library to access other basics (see errors in __construct() ):
( output: http://triv.ath.cx/ci3/ )

So, I'm at a loss as to what to do next... any advice?

CodeIgniter 3 Library:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require (APPPATH . '../vendor/autoload.php');

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

class CI_Xajax {

  public $xajax = '';

    public function __construct()
    {

    $this->xajax = Xajax::getInstance();
//        $this->_ci   =& get_instance();
        
    $this->xajax->setOption('core.debug.on', true);
    $this->xajax->setOption('core.prefix.function', 'xajax_');
        
    }
}

CodeIgniter 3 Welcome Controller:

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

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->library(array('CI_Xajax', 'session'));

//    $this->ci_xajax->xajax->register(Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Class 'Xajax' not found in /var/www/html/ci3/application/controllers/Welcome.php on line 12

//    $this->ci_xajax->xajax->register(CI_Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Undefined class constant 'USER_FUNCTION' in /var/www/html/ci3/application/controllers/Welcome.php on line 15

    }

    public function index()
    {
    $viewData = '';
    $viewData['XjxVersion'] = $this->ci_xajax->xajax->getVersion();

        $this->load->view('welcome_message',$viewData);
        
    }
    
    public function hello() {
        $this->load->view('welcome_message',$viewData);
    }
}

9 (edited by thierry.feuzeu 2016-04-26 4:31:08 PM)

Re: The new Xajax is out, and the new Xajax is cool!

Hi,

Although I've never used the CodeIgniter framework, IMO the main guidelines for integrating the Xajax library are the same as for other frameworks. So let me share what I'm currently doing with Laravel.

1. Choose a route for the Xajax request processing.

2. Choose a location and a namespace for the Xajax classes of the application. This step is optional.

3. Create an Xajax controller with at least two functions:
  - init(boolean): where the Xajax objects are created, configured and the Xajax classes are registered. The boolean parameter allows to skip the class registration.
Set the route chosen at step 1 as the 'core.request.uri' option of the Xajax library.

$xajax->setOption('core.request.uri', $route);

If a directory was chosen at step 2, all the classes inside can be registered here with two calls:

$xajax->addClassDir($sPath, $sNamespace);
$xajax->registerClasses();

  - process(): where the Xajax request is processed.
Here's a sample code for the process function.

    public function process()
    {
        $this->init(false); // There's no need to register all your classes when processing a single request
        // Process Ajax request
        $xajax = Xajax::getInstance():
        if($xajax->canProcessRequest())
        {
            $xajax->processRequest();
        }
    }

The Xajax controller may inherit from the base controller provided by the framework.

4. Redirect the route chosen in step 1 to the process() function.

5. Make any controller in your app serving Xajax powered pages inherit from the Xajax controller. This class must call the init() function above with its parameter set to true, before printing the page.

6. Output the Xajax generated code in your views using the $xajax->getJavascript(), $xajax->getJsInclude() and $xajax->getCssInclude() calls.

7. Take a fresh drink to relax after this hard work.

The steps 1 to 3, and even 4, can be implemented in a separated package, so the developer only need to make a few calls in its controller.
In this case, the config options to be set in the init() function may be loaded from the config system provided by the framework.

10 (edited by thierry.feuzeu 2016-04-26 12:09:47 PM)

Re: The new Xajax is out, and the new Xajax is cool!

moo wrote:

Hello All,

I'm trying to write a library for the newly resurrected Xajax project (from Thierry Feuzeu)...

and I'm coming up short due to my inexperience in writing CodeIgniter Libraries and with PHP namespacing.

Could anyone offer some thoughts on how to make this work? (see's Thierry's example code: https://github.com/lagdo/xajax-examples … /hello.php)

Here's what I've got so far, and it definitely instantiates an xajax object, of which I can access at least the getVersion() method from inside a CodeIgniter controller method, but I can't figure out how to write the Library to access other basics (see errors in __construct() ):
( output: http://triv.ath.cx/ci3/ )

So, I'm at a loss as to what to do next... any advice?

CodeIgniter 3 Library:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require (APPPATH . '../vendor/autoload.php');

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

class CI_Xajax {

  public $xajax = '';

    public function __construct()
    {

    $this->xajax = Xajax::getInstance();
//        $this->_ci   =& get_instance();
        
    $this->xajax->setOption('core.debug.on', true);
    $this->xajax->setOption('core.prefix.function', 'xajax_');
        
    }
}

CodeIgniter 3 Welcome Controller:

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

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->library(array('CI_Xajax', 'session'));

//    $this->ci_xajax->xajax->register(Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Class 'Xajax' not found in /var/www/html/ci3/application/controllers/Welcome.php on line 12

//    $this->ci_xajax->xajax->register(CI_Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Undefined class constant 'USER_FUNCTION' in /var/www/html/ci3/application/controllers/Welcome.php on line 15

    }

    public function index()
    {
    $viewData = '';
    $viewData['XjxVersion'] = $this->ci_xajax->xajax->getVersion();

        $this->load->view('welcome_message',$viewData);
        
    }
    
    public function hello() {
        $this->load->view('welcome_message',$viewData);
    }
}

Based on the above guidelines, I can suggest the following changes to your code

class CI_Xajax extends CI_Controller
{
// ...
}

class Welcome extends CI_Xajax
{
// ...
}

Also don't forget to declare the classes at top of your classes

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

11 (edited by moo 2016-04-26 6:20:46 PM)

Re: The new Xajax is out, and the new Xajax is cool!

Thanks Thierry!

The scope inside the base class doesn't seem to be able to see the code in the xajax use/includes.
The instantiating line results in:
// Result: Fatal error: Class 'Xajax\Xajax' not found in /var/www/html/ci3/application/core/MY_Controller.php on line 15

base controller: MY_Controller.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

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

class MY_Controller extends CI_Controller {

  public $xajax;
    
  public function __construct( ) {
    parent::__construct();
    $this->xajax = Xajax::getInstance();
    // Result: Fatal error: Class 'Xajax\Xajax' not found in /var/www/html/ci3/application/core/MY_Controller.php on line 15
  }
     
}

extending controller: Welcome.php

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

class Welcome extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
    // CodeIgniter's Hello World Controller/View
    $viewData = '';
        $this->load->view('welcome_message',$viewData);
        
    }
    
}

Re: The new Xajax is out, and the new Xajax is cool!

The Xajax classes cannot be found because the autoload is missing.

require (APPPATH . '../vendor/autoload.php');

Re: The new Xajax is out, and the new Xajax is cool!

Yes !!! It's a very good news !. I'm love Xajax and i never find alternative. I'm hope that you could maintain product, maybe find a community to help you smile

Re: The new Xajax is out, and the new Xajax is cool!

I've merged the xajax-config package into xajax-core. The xajax-config package implements functions to read the config options of the Xajax library from files in Php, Json and Yaml formats. So by now this feature is available in the xajax-core library out of the box.

I've added a new Config File example where the Xajax config is read from a Yaml file.
I've also modified the  Hello World Class and  Autoload Separated examples to read config resp. from a Php file and a Json file.

Re: The new Xajax is out, and the new Xajax is cool!

Hello Thierry,

I have used CI in the past and integrated Xajax by extending the CI controller. I am trying to do the same with the new Xajax.

MY_Controller:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
/*
    4/29/2016
    
    Author: Ed Robinson
    
    Demonstration CodeIgniter MY_Controller for implementing a sample CI app
    with xajax 0.7 alpha
*/
require (__DIR__ . '/../third_party/xajax/vendor/autoload.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;  //Xajax response instance
    
    public function __construct()
    {
        parent::__construct();
        $this->setupXajax();
    }
    
    public function setupXajax()
    {
        $this->xajax = Xajax::getInstance();
        //$this->resp = new Response();
        $this->xajax->setOption('core.prefix.function', 'xajax_');
    }
        
}

Testing MY_Controller - file MC_TEST:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
/*
    4/29/2016
    Test MY_Controller
*/
use Xajax\Response\Response;
class MC_Test extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->xajax->register(XAJAX_FUNCTION,'sayHello');
    }

    //Default method of the CI controllers
    function index()
    {
        $this->load->view('hello_world');
        $this->xajax->processRequest();
        
    }
    
    function sayHello()
    {
        $resp = new Response();
        $resp->alert("Hello World...");
        return $resp;
    }
    
    
}

View file hello_world.php:

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

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
</head>
<body>
    <input type="button" value="Click" onclick="xajax_sayHello()" />
<?php
    echo $this->xajax->getJsInclude();
    echo $this->xajax->getJavascript();
?>
</body>
</html>

I receive these errors:

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  call_user_func_array() expects parameter 1 to be a valid callback, function 'sayHello' not found or invalid function name</p>
<p>Filename: Support/UserFunction.php</p>
<p>Line Number: 242</p>

<p>Severity: 4096</p>
<p>Message:  Argument 1 passed to Xajax\Response\Manager::append() must be an instance of Xajax\Response\Response, null given, called in C:\wamp\www\ci-xajax0.7\application\third_party\xajax\src\Request\Support\UserFunction.php on line 242 and defined</p>
<p>Filename: Response/Manager.php</p>
<p>Line Number: 99</p>

I suspect the problem is in my not yet fully understandingthe new Xajax implementation...

Ed

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

Re: The new Xajax is out, and the new Xajax is cool!

Hello Ed,

Your sayHello() is actually a method, and thus cannot be registered with its function name.

You should either register the method with an alias

$this->xajax->register(XAJAX_FUNCTION,array('sayHello', $this, 'sayHello'));

Or declare the method in a separate class and register the class.

Re: The new Xajax is out, and the new Xajax is cool!

That works! Thanks Thierry.

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

Re: The new Xajax is out, and the new Xajax is cool!

moo,

I posted my CI with xajax0.7 in the examples section. Let me know what you think.
Ed

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

Re: The new Xajax is out, and the new Xajax is cool!

Thierry,

Is the new xajac php-7 compatible?

Thanks,
Ed

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

Re: The new Xajax is out, and the new Xajax is cool!

Ed,

The new library plays nicely with PHP7.

Thierry

Re: The new Xajax is out, and the new Xajax is cool!

Hi all,

The API documentation of the library is now online.
Check it out at http://xajax.lagdo-software.net/docs/api/

Thierry

Re: The new Xajax is out, and the new Xajax is cool!

Well done, Thierry. Thanks for all of your efforts.

Do you have any suggestions for a tool producing documentation examples? I would like to help with it.

Ed

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

Re: The new Xajax is out, and the new Xajax is cool!

Hi Ed,

Thanks for your help.
IMHO, MkDocs is a great tool to build the documentation.
It produces static HTML files from markdown-formatted text files, which can then be published on ReadTheDocs or Github Pages.

Regarding the doc content, having a separate Github project only for doc files seems to be a good idea too, as we could have not only the core package, but also some plugins and framework integration packages documented as well, all in a single project.

Thierry

Re: The new Xajax is out, and the new Xajax is cool!

I don't really understand your goal here ... You take something very easy to use and you changed it to something very bad and not friendly user at all.
You changed every name to be sure that all old code is no longer working.... and you did not do any documentation .... All your "example" is not working and you force programers to use the stupid tool composer ... Sorry to be rude, but can you simply start a new library ? I'm not sure that XAJAX should be the name of it ... You will mess up people, because it's totally not the same thing ...

Re: The new Xajax is out, and the new Xajax is cool!

Anyone speaking french here?

The french docs are online http://xajax.lagdo-software.net/docs/fr/, and the english docs are coming soon.