lagdo/jaxon-symfony

Jaxon library integration for the Symfony Framework


Keywords
framework, symfony, php, ajax, xajax, Jaxon
License
BSD-2-Clause

Documentation

Jaxon Library for Symfony

This package integrates the Jaxon library into the Symfony framework.

Features

  • Automatically register Jaxon classes from a pre-configured directory.
  • Read Jaxon options from a config file.

Installation

Add the following lines in the composer.json file, and run the composer update command.

"require": {
    "jaxon-php/jaxon-symfony": "~3.3"
}

Setup the default routing for Jaxon request by adding the following line in the config/routes.yml config file.

jaxon:
    resource: "@JaxonAjaxBundle/Resources/config/routes.yaml"

Import the service definition and configuration file of the Jaxon bundle in the config/services.yml config file.

imports:
    ...
    - { resource: jaxon.yaml }
    - { resource: "@JaxonAjaxBundle/Resources/config/services.yaml" }

Create and edit the config/jaxon.yaml file to suit the needs of your application. A sample config file is available in this repo.

This config file by default registers Jaxon classes in the jaxon/App directory with the \Jaxon\App namespace. Make sure this directory exists, even if it is empty.

Configuration

The settings in the config/jaxon.yml config file are separated into two sections. The options in the lib section are those of the Jaxon core library, while the options in the app sections are those of the Symfony application.

The following options can be defined in the app section of the config file.

Name Description
directories An array of directory containing Jaxon application classes
views An array of directory containing Jaxon application views

By default, the views array is empty. Views are rendered from the framework default location. There's a single entry in the directories array with the following values.

Name Default value Description
directory jaxon/App The directory of the Jaxon classes
namespace \Jaxon\App The namespace of the Jaxon classes
separator . The separator in Jaxon class names
protected empty array Prevent Jaxon from exporting some methods

Usage

This is an example of a Symfony controller using the Jaxon library.

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Jaxon\AjaxBundle\Jaxon;

class DemoController extends AbstractController
{
    /**
     * @Route("/", name="home")
     */
    public function index(Jaxon $jaxon)
    {
        return $this->render('demo/index.html.twig', [
            'jaxonCss' => $jaxon->css(),
            'jaxonJs' => $jaxon->js(),
            'jaxonScript' => $jaxon->script(),
        ]);
    }
}

Before it prints the page, the controller calls the $jaxon->css(), $jaxon->js() and $jaxon->script() functions to get the CSS and javascript codes generated by Jaxon, which it inserts into the page.

The Jaxon classes

The Jaxon classes can inherit from \Jaxon\CallableClass. By default, they are located in the jaxon/App dir of the Symfony application, and the associated namespace is \Jaxon\App.

This is a simple example of a Jaxon class, defined in the jaxon/App/HelloWorld.php file.

namespace Jaxon\App;

class HelloWorld extends \Jaxon\CallableClass
{
    public function sayHello()
    {
        $this->response->assign('div2', 'innerHTML', 'Hello World!');
        return $this->response;
    }
}

Request processing

By default, the Jaxon requests are handled by the controller in the src/Controller/JaxonController.php file. The /jaxon route is defined in the src/Resources/config/routes.yaml file, and linked to the JaxonController::index() method.

A route to a custom controller can be used, if for example event handlers needs to be defined.

Dependency injection

Services in Symfony can be declared as public or private, and injected in Jaxon classes.

Since Jaxon uses a container to fetch to the Symfony services that are injected in his classes, by default it will be able to get access only to services declared as public.

Starting from version 3.3.0, a service locator can be defined for Jaxon in the config/services.yaml file, in order to provide access to private services.

    jaxon.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'

The service locator must be declared as public, and take all the services that can be passed to Jaxon classes as arguments. See the Symfony service locators documentation.

Contribute

  • Issue Tracker: github.com/jaxon-php/jaxon-symfony/issues
  • Source Code: github.com/jaxon-php/jaxon-symfony

License

The package is licensed under the BSD license.