kenny1911/ajax-bundle
Composer 安装命令:
composer require kenny1911/ajax-bundle
包简介
Symfony bundle for help creating ajax endpoints
README 文档
README
Language: [ English | Русский ]
Description
kenny1911/ajax-bundle is a Symfony bundle that allows controllers marked with the #[Ajax] attribute to:
- Return DTOs instead of
Responseobjects. - 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.
统计信息
- 总下载量: 1.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-31