batenburg/response-factory-bundle

一个响应工厂包,用于清理您的控制器代码。

v1.1.8 2021-01-10 12:02 UTC

README

Symfony 框架的响应工厂。

构建状态: 构建状态

什么是响应工厂包?

响应工厂包是一个包含响应工厂的包,用于在您的 Symfony 控制器中创建干净代码。它受到 Laravel 中响应工厂的启发。

面向人群?

所有喜欢干净代码的人。

安装

使用 composer 安装

composer require batenburg/response-factory-bundle

注册包,将以下行添加到 config/bundles.php

    Batenburg\ResponseFactoryBundle\ResponseFactoryBundle::class => ['all' => true],

使用方法

安装完成后,可以通过依赖注入解析 ResponseFactoryInterface,或者通过容器。强烈建议使用依赖注入。

示例:

<?php

namespace App\Controller;

use Batenburg\ResponseFactoryBundle\Component\HttpFoundation\Contract\ResponseFactoryInterface;
use Batenburg\ResponseFactoryBundle\Component\HttpFoundation\Response;

class BrandController
{

    /**
     * @var ResponseFactoryInterface
     */
    private $responseFactory;

    /**
     * @param ResponseFactoryInterface $responseFactory
     */
    public function __construct(ResponseFactoryInterface $responseFactory)
    {
        $this->responseFactory = $responseFactory;
    }

    /**
     * @return Response
     */
    public function create(): Response
    {
        return $this->responseFactory->render('brand/create.html.twig');
    }
}

注意

要使用构造函数注入,必须在您的 yaml 文件中注册控制器:

App\Controller\BrandController:
    tags: ['controller.service_arguments']
    arguments:
        $responseFactory: '@batenburg.response_factory_bundle.component.http_foundation.response_factory'

在方法中使用依赖注入将被自动解析。

许可证

此缓存包是开源软件,使用 MIT 许可证发布。