在Symfony中使用Action类代替Controller

问题描述

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

为澄清起见,我已经在Zend Framework 2中使用过这种方法,但是它的名称Middleware。我在API平台中找到了类似的地方,他们也使用Action class代替Controller,但是问题是我不知道如何烹饪。

UPD: 如何获取一个Action类并替换标准Controller,以及应在常规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);
    }
}

解决方法

这个问题对于stackoverflow来说有点模糊,尽管它也很有趣。因此,这里有一些配置详细信息。

从现成的S4骨架项目开始:

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

添加SoftDeleteAction

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);
    }
}

并定义路线:

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

此时,接线几乎完成。如果您转到该网址,则会得到:

The controller for URI "/products/42/delete" is not callable:

原因是默认情况下服务是私有的。通常,您将从AbstractController扩展而来,该服务负责将服务公开,但是在这种情况下,最快的方法是将动作标记为控制器:

# config/services.yaml
    App\Action\Product\SoftDeleteAction:
        tags: ['controller.service_arguments']

这时您应该有一个有效的有线操作。

当然会有很多变化和更多细节。您将希望将路由限制为POST或伪造的DELETE。

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

但这足以让您入门。

,

我尝试实现的方法称为 ADR pattern(动作域响应器),Symfony已经从3.3版本开始支持该方法。您可以将其称为Invokable Controllers

摘自官方文档:

控制器还可以使用__invoke()方法定义单个动作,这是遵循ADR模式(Action-Domain-Responder)时的常见做法:

// 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));
    }
}