在 Symfony 中使用 Action 类代替 Controller

Ser*_*pov 6 action symfony symfony4

我坚持使用Action Class方法而不是Controller。解释很简单:控制器通常包含许多操作,当遵循依赖注入原则时,我们必须将所有必需的依赖项传递给构造函数,这会导致控制器具有大量依赖项的情况,但在某个时刻(例如请求)我们只使用一些依赖项。维护和测试意大利面条式的代码很困难。

澄清一下,我已经习惯在 Zend Framework 2 中使用这种方法,但它被命名为Middleware。我在 API-Platform 中发现了类似的东西,他们也使用Action 类而不是 Controller,但问题是我不知道如何烹饪它。

UPD:我如何获取下一个操作类并替换标准控制器以及我应该在常规 Symfony 项目中添加哪些配置?

<?php
declare(strict_types=1);

namespace App\Action\Product;

use App\Entity\Product;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class SoftDeleteAction
{
    /**
     * @var EntityManager
     */
    private $entityManager;

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

    /**
     * @Route(
     *     name="app_product_delete",
     *     path="products/{id}/delete"
     * )
     *
     * @Method("DELETE")
     *
     * @param Product $product
     *
     * @return Response
     */
    public function __invoke(Request $request, $id): Response
    {
        $product = $this->entityManager->find(Product::class, $id);
        $product->delete();
        $this->entityManager->flush();

        return new Response('', 204);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*pov 6

我试图实现的方法被命名为ADR 模式(Action-Domain-Responder),Symfony 从 3.3 版本开始就已经支持这种模式。您可以将其称为可调用控制器

来自官方文档:

控制器还可以使用 __invoke() 方法定义单个操作,这是遵循 ADR 模式(操作-域-响应程序)时的常见做法:

// src/Controller/Hello.php
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/hello/{name}", name="hello")
 */
class Hello
{
    public function __invoke($name = 'World')
    {
        return new Response(sprintf('Hello %s!', $name));
    }
}

Run Code Online (Sandbox Code Playgroud)


Cer*_*rad 4

这个问题对于 stackoverflow 来说有点模糊,尽管它也有点有趣。所以这里有一些配置细节。

从开箱即用的 S4 骨架项目开始:

symfony new --version=lts s4api
cd s4api
bin/console --version # 4.4.11
composer require orm-pack

Run Code Online (Sandbox Code Playgroud)

添加软删除操作

namespace App\Action\Product;
class SoftDeleteAction
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function __invoke(Request $request, int $id) : Response
    {
        return new Response('Product ' . $id);
    }
}
Run Code Online (Sandbox Code Playgroud)

并定义路线:

# config/routes.yaml
app_product_delete:
    path: /products/{id}/delete
    controller: App\Action\Product\SoftDeleteAction

Run Code Online (Sandbox Code Playgroud)

至此,接线已基本完成。如果您访问该网址,您会得到:

The controller for URI "/products/42/delete" is not callable:
Run Code Online (Sandbox Code Playgroud)

原因是服务默认是私有的。通常,您可以从 AbstractController 进行扩展,它负责使服务公开,但在这种情况下,最快的方法是将操作标记为控制器:

# config/services.yaml
    App\Action\Product\SoftDeleteAction:
        tags: ['controller.service_arguments']
Run Code Online (Sandbox Code Playgroud)

此时,您应该有一个可以工作的连线动作。

当然还有很多变化和更多细节。您需要将路由限制为 POST 或假 DELETE。

您还可以考虑添加一个空的 ControllerServiceArgumentsInterface,然后使用 services instanceof 功能来应用控制器标记,这样您就不再需要手动定义控制器服务。

但这应该足以让您开始。