繁体   English   中英

Symfony 4.4 如何在没有自动装配的情况下配置控制器

[英]Symfony 4.4 how to configure controllers without autowiring

我需要向控制器构造函数方法注入服务

服务.yaml

imports:
    - { resource: controllers.yaml }

services:
    _defaults:
        autowire: false
        autoconfigure: false 
        public: false

    App\:
        resource: '../src/*'
        exclude: '../src/{Application/Message,Infrastructure/Repository/MySql/Migrations,Tests,Kernel.php}'

控制器.yaml

    app.controller:
        class: App\UI\Controller\AppController
        arguments:
            - '@monolog.logger.api'
        tags: ['controller.service_arguments']

    app.controller.keyword:
        class: App\UI\Controller\BlogController
        arguments:
            - '@monolog.logger.api'
        tags: ['controller.service_arguments']

博客控制器.php

class BlogController extends AbstractController
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function postForSubscribers(Request $request, FindPostQuery $query): JsonResponse
    {
        $page = $request->query->get('page') ?? 1;
        $limit = $request->query->get('limit') ?? 500;
        $daysBack = $request->query->get('days-back') ?? '7';
        try {
            $results = $query->getResults($daysBack, new Paging((int)$page, (int)$limit));
            return new JsonResponse($results->normalize());
        } catch (Exception $e) {
            $this->logger->warning($e->getMessage(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
                'trace' => $e->getTraceAsString(),
            ]);
            return new JsonResponse(['error' => 'Something broken, cant fetch post data.'], 500);
        }
    }
}

操作postForSubscribers返回The controller for URI "/api/v1/post/subscribers" is not callable. Controller "App\\UI\\Controller\\BlogController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"? The controller for URI "/api/v1/post/subscribers" is not callable. Controller "App\\UI\\Controller\\BlogController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

但是controllers.yaml 包含控制器标签,我应该如何配置我的控制器呢? 任何人都可以帮忙吗?

附注。 当我添加到 service.yaml 时:

App\UI\Controller\:
        resource: '../src/UI/Controller'
        tags: ['controller.service_arguments']

我得到: Too few arguments to function App\\UI\\Controller\\BlogController::__construct(), 0 passed in /var/www/html/var/cache/dev/ContainerV9lwkz0/getBlogControllerService.php on line 13 and exactly 1 expected

正如我所看到的,您没有从默认配置中排除您的控制器,请在此处添加排除项:

exclude: '../src/{Application/Message,Infrastructure/Repository/MySql/Migrations,Tests,Kernel.php}'

这不是必需的,但这可能会覆盖您的手动配置。 (这取决于哪个是第一个)。

然后,关注autowire参数,如果此参数为 false,则必须使用arguments手动注入依赖项 - 请参阅autowiringservice container

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM