Symfony 4不自动装配动态路由控制器

时间:2018-08-14 22:49:41

标签: php symfony routing autowired symfony4

Symfony版本:4.1.3

我有一个应用程序通过路由加载器服务基于配置加载动态路由,但是看来Symfony 4并未自动装配动态路由控制器。

我正在使用标准的Symfony 4应用程序services.yaml文件:

/config/services.yaml

parameters:
services:
   # default configuration for services in *this* file
  _defaults:
     autowire: true      # Automatically injects dependencies in your services.
     autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
     public: false       # Allows optimizing the container by removing unused services; this also means
                         # fetching services directly from the container via $container->get() won't work.
                         # The best practice is to be explicit about your dependencies anyway.

  # makes classes in src/ available to be used as services
  # this creates a service per class whose id is the fully-qualified class name
  Application\Web\:
    resource: '../src/*'
    exclude: '../src/{Search/Model,Entity,Migrations,Tests,Kernel.php}'

  # controllers are imported separately to make sure services can be injected
  # as action arguments even if you don't extend any base controller class
  Application\Web\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

路由加载器: src/Component/RouteLoader.php

<?php

namespace Application\Web\Component;

use Application\Symfony\Bundle\ConfigBundle\ReaderInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Class RouteLoader
 * @package Application\Web\Component
 */
class RouteLoader
{
    /**
     * @var ReaderInterface
     */
    private $configurationReader;

    public function __construct(ReaderInterface $configurationReader)
    {
        $this->configurationReader = $configurationReader;
    }

    public function load(): RouteCollection
    {
        $routes = new RouteCollection();
        foreach ($this->configurationReader->find('category_navigation') as $label => $configuration) {
            $slug = strtolower($label);
            $route = new Route(
                $configuration['url'],
                [
                    '_controller' => [$configuration['controller'], 'dispatch'],
                    'categories_slug' => $slug,
                    'category_label' => $label,
                    'page_title' => $configuration['page_title'] ?? null,
                    'page_description' => $configuration['page_description'] ?? null,
                ],
                [],
                [],
                '',
                [],
                ['GET']
            );
            $routes->add($slug . '.home', $route);
        }

        return $routes;
    }
}

控制器构造函数: src/Controller/Page.php

<?php

namespace Application\Web\Controller;

//.... other class code

public function __construct(
    ClientInterface $client,
    ReaderInterface $configurationReader,
    \Twig_Environment $twigEnvironment,
    ContentSearcher $contentSearcher,
    Environment $environment,
    TokenStorageInterface $tokenStorage,
    UrlGeneratorInterface $urlGenerator
)

当我尝试加载页面时,Symfony发出以下错误提示:

Controller "\Application\Web\Controller\Page" has required constructor arguments and does not exist in the container. Did you forget to define such a service?

但是,当我直接在config/routes.yaml文件中定义路由时,控制器将自动进行样式自动接线!

我的问题是:

  • 这是否是Symfony自动装配功能的限制,即动态路由控制器不支持该功能?
  • 在定义使自动装配工作的路线时,我是否缺少某些东西?
  • 我有可能确定一个错误吗?

有什么想法吗?

编辑:堆栈跟踪

InvalidArgumentException:
Controller "\Application\Web\Controller\Page" has required constructor arguments and does not exist in the container. Did you forget to define such a service?

  at vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:62
  at Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->instantiateController('\\Application\\Web\\Controller\\Page')
 (vendor/symfony/framework-bundle/Controller/ControllerResolver.php:54)
  at Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->instantiateController('\\Application\\Web\\Controller\\Page')
 (vendor/symfony/http-kernel/Controller/ControllerResolver.php:49)
  at Symfony\Component\HttpKernel\Controller\ControllerResolver->getController(object(Request))
 (vendor/symfony/http-kernel/Controller/TraceableControllerResolver.php:38)
  at Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController(object(Request))
 (vendor/symfony/http-kernel/HttpKernel.php:132)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
 (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
 (vendor/symfony/http-kernel/Kernel.php:188)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
 (public/index.php:37)


 ArgumentCountError:
Too few arguments to function Application\Web\Controller\Base::__construct(), 0 passed in /var/www/html/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 133 and exactly 7 expected

  at src/Controller/Base.php:55
  at Application\Web\Controller\Base->__construct()
 (vendor/symfony/http-kernel/Controller/ControllerResolver.php:133)
  at Symfony\Component\HttpKernel\Controller\ControllerResolver->instantiateController('\\Application\\Web\\Controller\\Page')
 (vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:55)
  at Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->instantiateController('\\Application\\Web\\Controller\\Page')
 (vendor/symfony/framework-bundle/Controller/ControllerResolver.php:54)
  at Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->instantiateController('\\Application\\Web\\Controller\\Page')
 (vendor/symfony/http-kernel/Controller/ControllerResolver.php:49)
  at Symfony\Component\HttpKernel\Controller\ControllerResolver->getController(object(Request))
 (vendor/symfony/http-kernel/Controller/TraceableControllerResolver.php:38)
  at Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController(object(Request))
 (vendor/symfony/http-kernel/HttpKernel.php:132)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
 (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
 (vendor/symfony/http-kernel/Kernel.php:188)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
 (public/index.php:37)

1 个答案:

答案 0 :(得分:2)

回答您的问题:

  1. 否,至少在这种情况下不行。路由加载程序的唯一作用是构建路由集合,以便可以将请求与之匹配。通过提供_controller参数,您可以告诉SF哪个控制器映射到该路由。依赖注入组件是负责将控制器作为服务自动加载到容器中的组件。但是,如果控制器到达ContainerControllerResolver并从第50-52行经过,则绝对是由于您的控制器未注册为服务(或更确切地说,无论{的值是多少) $class中的{1}}在容器中不存在。
  2. 我无法复制,因为我没有您的服务。但我最大的猜测是,将ContainerControllerResolver::instantiateController()参数作为某种可调用数组传递可能不起作用。尝试将其作为字符串传递,例如_controllerApplication/Web/Pages::instance可以使用它。
  3. 我有疑问,但这很可能。

能够看到堆栈跟踪将非常有帮助。

更新:

与您的类名称相符的字符串中有两个反斜杠。尝试重新格式化为:ContainerControllerResolver

如果您想在重构之前确定此修复程序,请运行Application\Web\Controller\Page并检查作为服务的Page控制器是否存在。如果是这样,那么问题肯定是带有双反斜杠的FQCN。