qpautrat / woohoolabs-yin-bundle
将woohoolabs/yin框架集成到Symfony中
6.0.0
2019-12-23 15:46 UTC
Requires
- php: ^7.1.0
- psr/http-factory: ^1.0
- sensio/framework-extra-bundle: ^5.4
- symfony/psr-http-message-bridge: ^1.2
- woohoolabs/yin: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- nyholm/psr7: ^1.2
- phpspec/phpspec: ^4.3
README
此包将woohoolabs/yin库集成到Symfony框架中。
注意:5.x
适用于Yin 4.x
,4.x
适用于Yin 3.x
,3.x
适用于Yin 0.11
安装
$ composer require qpautrat/woohoolabs-yin-bundle
然后对于Symfony 3.x及之前版本,与任何其他包一样,将其包含在您的Kernel类中
public function registerBundles() { $bundles = array( // ... new QP\WoohoolabsYinBundle\QPWoohoolabsYinBundle(), ); // ... }
Symfony 4+将自动注册该包。
配置
默认情况下,jsonApi
类使用Yin的ExceptionFactory
初始化。您可以提供自己的工厂实现。为此,您需要在全局配置中定义要使用哪个服务,如下所示
qp_woohoolabs_yin: exception_factory: my_exception_factory_service
用法
配置服务绑定
services: _defaults: #... bind: WoohooLabs\Yin\JsonApi\JsonApi: '@qp_woohoolabs_yin.json_api'
然后您可以通过构造函数注入使用qp_woohoolabs_yin.json_api
服务
namespace App\Controller; use Psr\Http\Message\ResponseInterface; use WoohooLabs\Yin\JsonApi\JsonApi; class DefaultController { /** * @var JsonApi */ private $jsonApi; public function __construct(JsonApi $jsonApi) { $this->jsonApi = $jsonApi; } public function index(): ResponseInterface { return $this->jsonApi->respond()->ok(new HelloDocument(), 'hello'); } }
或在动作方法中直接使用
namespace App\Controller; use Psr\Http\Message\ResponseInterface; use WoohooLabs\Yin\JsonApi\JsonApi; class DefaultController { public function index(JsonApi $jsonApi): ResponseInterface { return $jsonApi->respond()->ok(new HelloDocument(), 'hello'); } }