关于 symfony 3.4.8 服务自动装配

About symfony 3.4.8 service autowiring

我使用的是 symfony 版本 3.4.8。 该配置在 symfony 3.3 中有效,但在 symfony 3.4 中我得到:

Cannot autowire service "ProblemBundle\EntityManager\ProblemManager": argument "$paginator" of metho  
  d "__construct()" references interface "Knp\Component\Pager\PaginatorInterface" but no such service   
  exists. Did you create a class that implements this interface?

ProblemManager.php

public function (PaginatorInterface $page){}

app/config/services.yml

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: true

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    # add more services, or override services that need manual wiring
    # AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'


    UserBundle\Controller\:
        resource: '../../src/UserBundle/Controller'
        public: true
        autowire: true
        tags: ['controller.service_arguments']

    UserBundle\EntityManager\UserManager:
        autowire: true

    ProblemBundle\EntityManager\ProblemManager:
        autowire: true
        public: true

如何为所有(包括第三方包)配置自动装配?

指示依赖注入组件您想使用哪个实现,例如:

Knp\Component\Pager\PaginatorInterface:
    public: true
    alias: Your\Solid\Implementation

如果您想使用其他实现,只需更改这一配置行即可。

您仍然可以通过服务标签注入 Non-Autowireable 参数,在您的情况下是

   ProblemBundle\EntityManager\ProblemManager:
    autowire: true
    public: true
    arguments:
        $page: "@knp_paginator.injectable"

参见:https://github.com/KnpLabs/KnpPaginatorBundle