php - Symfony 4 不 Autowiring 动态路由 Controller

标签 php symfony routes autowired symfony4

Symfony 版本:4.1.3

我有一个应用程序,它通过路由加载器服务根据配置加载动态路由,但 Symfony 4 似乎没有 Autowiring 动态路由 Controller 。

我正在使用标准 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;
    }
}

Controller 构造函数: 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 文件中定义路由时, Controller 会自动连接,没有任何问题!

我的问题是:

  • 这是否是 Symfony Autowiring 功能的限制,即不支持动态路由 Controller ?
  • 在定义使 Autowiring 工作的路由时,我是否遗漏了什么?
  • 我是否可能发现了错误?

有什么想法吗?

编辑:堆栈跟踪

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. 不,至少在这种情况下不是。路由加载器的唯一作用是构建路由集合,以便可以将请求与其进行匹配。通过提供 _controller 参数,您可以告诉 SF 哪个 Controller 映射到该路由。依赖注入(inject)组件负责将 Controller 作为服务自动加载到容器中。但是,如果 Controller 到达 ContainerControllerResolver 并从第 50-52 行传递,则肯定是由于您的 Controller 未注册为服务(或者更准确地说,无论是什么) ContainerControllerResolver::instantiateController() 中的 $class 值在容器中不存在。
  2. 我无法复制,因为我没有你们的服务。但我最好的猜测是,将 _controller 参数作为某种可调用数组传递可能不起作用。尝试将其作为字符串传递,例如 Application/Web/Pages::instanceContainerControllerResolver 可以使用它。
  3. 我有疑问,但这是有可能的。

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

更新:

字符串中有双反斜杠,与您的类名相符。尝试重新格式化为:Application\Web\Controller\Page

如果您想在重构之前确定此修复,请运行 bin/console debug:container Page 并检查您的页面 Controller 作为服务是否存在。如果确实如此,那么问题肯定是带有双反斜杠的 FQCN。

关于php - Symfony 4 不 Autowiring 动态路由 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51850741/

相关文章:

javascript - ajax 成功的数组将数据放入表中的相应输入中

laravel-5 - 在 Laravel 的单个路由组中添加多个前缀

python - 使用 Python Bottle 框架自动测试路线

php - 交响乐 2 : Setting a cookie for the current session upon login

templates - Symfony 2 中的模板部分

symfony - 无法使用 Request 类检索 get 参数

c++ - 如何使用自定义消息扩展 Inet 的不同模块?

PHPBB登录实现

javascript - 修改脚本以折叠提交表单上的 div

php - 我的代码意外结束