kenny1911 / ajax-bundle
Symfony bundle for help creating ajax endpoints
Installs: 140
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: 8.1.* || 8.2.* || 8.3.* || 8.4.*
- symfony/config: ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/error-handler: ^5.4 || ^6.0 || ^7.0
- symfony/event-dispatcher: ^5.4 || ^6.0 || ^7.0
- symfony/http-foundation: ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.0 || ^7.0
- symfony/serializer: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^12.0
This package is auto-updated.
Last update: 2025-03-31 19:34:40 UTC
README
Language: [ English | Русский ]
Description
kenny1911/ajax-bundle
is a Symfony bundle that allows controllers marked with the #[Ajax]
attribute to:
- Return DTOs instead of
Response
objects. - Automatically convert controller responses to JSON.
- Convert exceptions thrown in the controller to JSON responses.
Together with Symfony Mapping Request Data, it can be used as a simpler alternative to FOS REST Bundle.
The symfony/serializer
package is used for serialization. Serialization is performed only in JSON format.
Installation
composer require kenny1911/ajax-bundle
Add Kenny1911\AjaxBundle\AjaxBundle
to bundles.php
.
Usage
Simply add the #[Ajax]
attribute to a controller method, and the returned DTO will automatically be converted to JSON.
Example Usage
use Kenny1911\AjaxBundle\Attribute\Ajax; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; final class PostController { public function __construct( private readonly PostRepository $postRepository, ) {} #[Ajax] public function getPost(string $id): Post { $post = $this->postRepository->find($id) ?? throw new NotFoundHttpException(); return new Post( id: $post->getId(), title: $post->getTitle(), ); } }
The client will receive the following JSON response:
{ "id": 1, "title": "Post title" }
Compatibility
- PHP 8.1+
- Symfony 5.4+
License
This project is licensed under the MIT License. See the LICENSE file for details.