azjezz / input-hydrator-bundle
从请求输入中为输入 DTOs 进行加湿。
1.0.2
2020-11-01 12:12 UTC
Requires
- php: >=7.4
- azjezz/input-hydrator: ^1.0
- symfony/framework-bundle: ^4.4 || ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ^9.4
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.1
This package is auto-updated.
Last update: 2024-08-29 05:07:28 UTC
README
关于
InputHydratorBundle 为 azjezz/input-hydrator
包提供 Symfony Bundle。
安装
要安装此 Bundle,运行以下命令并将获得最新版本
$ composer require azjezz/input-hydrator-bundle
配置
配置输入加湿器 Bundle 非常直接,你只需要将 Bundle 添加到你的 config/bundles.php
注意:如果你已安装
symfony/flex
,这将自动完成。
// config/bundles.php <?php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], ... AzJezz\Input\HydratorBundle\InputHydratorBundle::class => ['all' => true], ];
这样就完成了。
用法
要使用输入加湿器,你首先需要创建你的输入 DTO 类。
例如
// src/Input/Search.php <?php declare(strict_types=1); namespace App\Input; use AzJezz\Input\InputInterface; final class Search implements InputInterface { public string $query; }
之后,你可以在控制器中作为参数请求 DTO
// src/Controller/SearchController.php <?php declare(strict_types=1); namespace App\Controller; use App\Input\Search; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; final class SearchController { /** * @Route("/search", methods={"GET"}) */ public function index(Search $search): Response { return new Response('You were looking for "'.$search->query.'"?'); } }
使用 Symfony 的参数解析器,此 Bundle 能够加湿 Search
DTO 并将其传递到你的控制器。
如果请求没有指定 query
字段,或者 query
包含其他类型(例如 array
),参数解析器将抛出 BadRequestHttpException
,这将导致 400 Bad Request
响应。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE
。