dbstudios / doze-bundle
一个易于使用的库,用于在Symfony中构建REST API
2.0.0
2018-10-19 14:00 UTC
Requires
- php: ^7.2
- dbstudios/doze: ^1.0
- symfony/dependency-injection: ^4.1
Requires (Dev)
- lexik/jwt-authentication-bundle: ^2.5
- nelmio/cors-bundle: ^1.5
- phpunit/phpunit: 5.7.*
Suggests
- lexik/jwt-authentication-bundle
- nelmio/cors-bundle: If you need handle CORS requests
README
$ composer require dbstudios/doze-bundle
配置
将以下内容添加到app/AppKernel.php
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { // ... $bundles = [ // ... new DaybreakStudios\DozeBundle\DaybreakStudiosDozeBundle(), ]; // ... return $bundles; } // ... }
您还需要在您的bundle的services.yml
中添加对响应者的服务定义
services: app.doze.responder: class: DaybreakStudios\DozeBundle\ResponderService arguments: - '@app.serializer' - '@request_stack' app.serializer: class: Symfony\Component\Serializer\Serializer arguments: - - '@app.serializer.normalizer.datetime' - '@app.serializer.normalizer.collection' - '@app.serializer.normalizer.entity' - '@app.serializer.normalizer.object' - - '@app.serializer.encoder.json' app.serializer.normalizer.datetime: class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer # This normalizer is only necessary if you plan on normalizing # Doctrine collections to paged collections. # @see https://github.com/LartTyler/Doze#paged-collections app.serializer.normalizer.collection: class: DaybreakStudios\Doze\Serializer\PagedCollectionNormalizer arguments: - 25 # This normalizer is only necessary if you plan on normalizing database entities. # @see https://github.com/LartTyler/Doze#serializing-database-objects app.serializer.normalizer.entity: class: DaybreakStudios\Doze\Serializer\EntityNormalizer app.serializer.normalizer.object: class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer app.serializer.encoder.json: class: Symfony\Component\Serializer\Encoder\JsonEncoder
可选地,如果需要修改Doze的默认行为,您也可以将以下参数之一或全部添加到您的应用程序中。
// app/config/parameters.yml parameters: # Whether or not to use the built-in CORS listener. If disabled, Doze will not be able to respond to # CORS requests unless you add the necessary CORS headers yourself. dbstudios.cors_listener.enabled: true # An array of origins that are allowed to send CORS requests. # If the only value in this array is an asterisk, all origins will be allowed. dbstudios.cors_listener.allowed_origins: - '*' # An array of headers that are allowed in CORS requests. # An asterisk denotes all headers are allowed. dbstudios.cors_listener.allowed_headers: - '*' # An array of methods that are allowed in CORS requests. # An asterisk denotes all methods are allowed. dbstudios.cors_listener.allowed_methods: - '*' # A boolean indicating whether or not credentials are allowed in CORS requests. dbstudios.cors_listener.allow_credentials: true
上述所有参数都显示了它们的默认值。只有当您需要更改上述显示的内容时,才需要包含参数。
用法
在下面的示例中,响应者将被用于在控制器操作中序列化来自Doctrine的数据。然而,Doze可以用于序列化任何类型的数据,并且可以在任何可以访问服务容器的地方或通过依赖注入使用。
<?php use Symfony\Bundle\FrameworkBundle\Controller\Controller; use DaybreakStudios\DozeBundle\ResponderService; class MyController extends Controller { public function indexAction(ResponderService $responder, $id) { $entity = $this->getDoctrine()->getRepository('AppBundle:MyEntity')->find($id); if ($entity === null) return $responder->createNotFoundResponse(); return $responder->createResponse($entity); } }