peedro07/symfony-api-rest-bundle
最新稳定版本:1.5.0
Composer 安装命令:
composer require peedro07/symfony-api-rest-bundle
包简介
This bundle, with Symfony, makes it easier to manage the rendering of your data
README 文档
README
Bundle to simplify Symfony API creation
Version 2.0. of the bundle works with Symfony 7.x.x
This bundle streamlines API development using convenient annotations. It requires the extension of AbstractPGTRest.
Getting Started
To begin, extend the bundle in your controller:
class UserController extends AbstractPGTRest
Ensure the responseOptionsService is initialized by invoking the parent constructor:
parent::__construct($responseOptionsService);
Example Usage
Utilize the bundle through an illustrative example:
//UserEntity use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Attribute\Groups; #[Groups(["user:read"])] // Initialize the group for serialization #[ORM\Column(length: 255)] private ?string $name = null;
//UserController use App\Repository\UserRepository; use PGTRest\Attribute\ResponseOptions; use PGTRest\Controller\AbstractPGTRest; use PGTRest\Service\ResponseOptionsService; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; public function __construct(ResponseOptionsService $responseOptionsService) { parent::__construct($responseOptionsService); } #[Route('/users', name: 'app_users_get', methods: ["GET"])] #[ResponseOptions(statusCode: 200, groups: ["user:read"], formatDate: 'Y-m-d']) // Set the response options with the desired status code and serialization group public function index(UserRepository $userRepository): Response { $users = $userRepository->findAll(); return $this->view($users); //option 1 //return $this->view(['all_users' => $users]); option 2 }
//option 1 { "users": [ { "name": "John" }, { "name": "Jane" } ] }
//option 2 { "all_users": [ { "name": "John" }, { "name": "Jane" } ] }
Note: The view method accepts an array of data and optional parameters for $statusCode and $groups
Additional Features
When the formatDate option is set, the bundle will format the date fields in the response according to the specified format.
#[ResponseOptions(statusCode: 200, groups: ["user:read"], formatDate: 'Y-m-d'])
统计信息
- 总下载量: 50
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-17