jaxon-php / jaxon-symfony
Jaxon library integration for the Symfony Framework
Installs: 1 244
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 3
Open Issues: 0
Requires
- jaxon-php/jaxon-core: ^4.0
- symfony/expression-language: *
- symfony/framework-bundle: 5.*|6.*
Provides
- dev-main
- v4.x-dev
- v4.0.0
- v4.0-rc.1
- v4.0-beta.1
- v3.4.x-dev
- v3.4.0
- v3.3.0
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.0
- v2.1.0
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0-beta.6
- v2.0-beta.5
- v2.0-beta.4
- v2.0-beta.3
- v2.0-beta.2
- v2.0-beta.1
- v1.0.x-dev
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-analysis-8bR4x4
This package is auto-updated.
Last update: 2024-11-15 19:50:29 UTC
README
This package integrates the Jaxon library into the Symfony framework.
Installation
Add the following lines in the composer.json
file, and run the composer update
command.
"require": { "jaxon-php/jaxon-symfony": "^5.0" }
Or run the composer require jaxon-php/jaxon-symfony ^5.0
command.
Add the Jaxon bundle in the config/bundle.php
file.
return [ ... Jaxon\Symfony\JaxonBundle::class => ['all' => true], ];
Create and edit the packages/config/jaxon.yaml
file to suit the needs of your application.
A sample config file is available in this repo.
Add the following settings in the config/services.yaml
file, to configure the Jaxon library.
imports: ... - { resource: packages/jaxon.yaml }
This config file by default registers Jaxon classes in the jaxon/ajax
directory with the \Jaxon\Ajax
namespace.
The Jaxon library must be setup on all pages that need to show Jaxon related content, using an event subscriber for example.
<?php // src/EventSubscriber/JaxonSubscriber.php namespace App\EventSubscriber; use App\Controller\DemoController; use App\Controller\JaxonController; use Jaxon\Symfony\App\Jaxon; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\HttpKernel\KernelEvents; use function is_array; class JaxonSubscriber implements EventSubscriberInterface { public function __construct(private Jaxon $jaxon) {} public function onKernelController(ControllerEvent $event) { $controller = $event->getController(); // when a controller class defines multiple action methods, the controller // is returned as [$controllerInstance, 'methodName'] if (is_array($controller)) { $controller = $controller[0]; } // Select the controllers with Jaxon related content. if ($controller instanceof JaxonController || $controller instanceof DemoController) { $this->jaxon->setup(); } } public static function getSubscribedEvents() { return [ KernelEvents::CONTROLLER => 'onKernelController', ]; } }
Define a controller action to process Jaxon ajax requests.
use Jaxon\Symfony\App\Jaxon; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class JaxonController extends AbstractController { #[Route('jaxon', name: 'jaxon.ajax', methods: ['POST'])] public function __invoke(Jaxon $jaxon) { if(!$jaxon->canProcessRequest()) { return; // Todo: return an error message } $jaxon->processRequest(); return $jaxon->httpResponse(); } }
Insert Jaxon js and css codes in the pages that need to show Jaxon related content, using the Twig
functions provided by the Jaxon bundle.
use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use function Jaxon\rq; class DemoController extends AbstractController { #[Route('/', name: 'demo.home')] public function __invoke() { // Print the page return $this->render('demo/index.html.twig', [ 'pageTitle' => "Symfony Framework", ]); } }
// templates/demo/index.html.twig <!-- In page header --> {{ jxnCss() }} </head> <body> <!-- Page content here --> </body> <!-- In page footer --> {{ jxnJs() }} {{ jxnScript() }}
Configuration
The settings in the config/package/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.
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.
Usage
The Jaxon classes
The Jaxon classes can inherit from \Jaxon\App\CallableClass
.
By default, they are located in the jaxon/ajax
dir of the Symfony application, and the associated namespace is \Jaxon\Ajax
.
This is a simple example of a Jaxon class, defined in the jaxon/Ajax/HelloWorld.php
file.
namespace Jaxon\Ajax; class HelloWorld extends \Jaxon\App\CallableClass { public function sayHello() { $this->response->assign('div2', 'innerHTML', 'Hello World!'); return $this->response; } }
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.
A service locator can be defined for Jaxon in the config/services.yaml
file, in order to provide access to private services.
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.