【发布时间】: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;
}
}
输出:
布尔(假)
为什么我注册了两个服务而不是一个?
【问题讨论】: