【问题标题】:Why does symfony container has service duplicates?为什么 symfony 容器有服务重复?
【发布时间】:2018-04-12 10:25:47
【问题描述】:

我想将App\Service\SomeService 类注册为服务。

这是我的services.yaml

services:
    _defaults:
        autowire: false
        autoconfigure: true
        public: true
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
    someservice:
        class: App\Service\SomeService

现在我运行debug:container someservice:

Information for Service "someservice"
=====================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       someservice              
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 

而且,当我运行debug:container App\\Service\\SomeService

Information for Service "App\Service\SomeService"
=================================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       App\Service\SomeService  
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 

原来我有另一个服务指向同一个类:

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Service\SomeService;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     * @return Response
     */
    public function index()
    {
        var_dump($this->get('someservice') === $this->get(SomeService::class));
        return new Response;
    }
}

输出:

布尔(假)

为什么我注册了两个服务而不是一个?

【问题讨论】:

    标签: php symfony symfony4


    【解决方案1】:

    那是因为您在 services.yaml 中注册了两次。

    使用此行自动一次:

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
    

    另一个,手动:

    someservice:
        class: App\Service\SomeService
    

    由于它们有不同的名称(someserviceApp\Service\SomeService),它们对于 Symfony 来说是不同的。

    我建议您删除第二个声明,并仅在控制器中使用其完全限定名称调用服务:$this->get(SomeService::class)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多