alks / http-extra-bundle
为 symfony 框架提供的插件,包括控制器参数绑定到请求体/查询,内容协商等
v0.2.0
2017-12-27 20:50 UTC
Requires
- php: >=5.6.0
- doctrine/annotations: ^1.0
- symfony/framework-bundle: ~3.1|~4.0
- willdurand/negotiation: 2.2.*
Requires (Dev)
- symfony/expression-language: ~3.1|~4.0
- symfony/serializer: ~3.1|~4.0
This package is not auto-updated.
Last update: 2024-09-15 02:08:38 UTC
README
为 Symfony3 框架添加以下注解
- @RequestParam : 将任何查询参数与操作参数匹配。
- @RequestBody : 将请求体(内容)与操作参数匹配。
- @RequestData : 将任何请求数据参数($request->request)与操作参数匹配。
- @Response : 为响应对象提供额外信息(头等)。
此包还包括
- 自动反序列化和验证请求体/数据到操作参数
- 当操作返回除 Response 对象之外的内容时,生成响应
- 从查询参数解析 doctrine 实体
- 自动将请求内容规范化到类中
(如果配置正确),使控制器仅与有效的结构化请求数据交互。当你喜欢使用 DTO 而不是表单时很有用。
需求
- PHP 5.4 或更高版本
- Symfony 3.1 或更高版本
此包使用在 Symfony 3.1 中引入的值解析器
安装
- 通过 packagist 安装包
$ composer require alks/http-extra-bundle
36. 在你的 symfony 项目中启用此包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Alks\HttpExtraBundle\HttpExtraBundle(), // ... ); }
基本示例
请注意以下示例仅演示了包的基本用法,不适用于实际应用。有关更详细的示例,请参阅文档。
<?php use Alks\HttpExtraBundle\Annotation as Http; class FooController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller { /** * This will match the "/user?username=foo" and will automatically call the user repository to find a user with the foo * username. * * @Route("/user", methods={"GET"}) * @Http\RequestParam(name="username", bindTo="user", repository="AppBundle\Repository\UserRepository") * @return User */ public function getUserByUsernameAction(User $user) { return $user; } /** * This will match a GET request with optional page and limit query parameters like "/posts?page=3&limit=20" * * @Route("/posts", methods={"GET"}) * @Http\RequestParams({"page","limit"}) * @Http\Response(context={"groups":{"list"}}, type="json") */ public function getPostsAction($page=1, $limit=10) { return $this->getDoctrine()->getRepository('AppBundle:Post')->findAllByPage($page,$limit); } }
了解更多关于此包的信息 这里