[英]Symfony 2 calling non-existent service “router”
I have a service MailController
which is defined like this in my config 我有一个服务
MailController
,它在我的配置中定义为
services:
mail_controller:
class: Company\Project\Bundle\Controller\MailController
I'm calling the Service in other services 我在其他服务中呼叫服务
$mailController = $this->get('mail_controller');
Now the error i get is building up on this Question 现在我得到的错误是建立在这个问题上
The container
wasn't set on the Controller, so i'm injecting one within the constructor
该
container
未在Controller上设置,因此我在constructor
注入了一个
// MailController
public function __construct() {
$this->setContainer(new Container());
}
Now i'm getting this error: 现在我得到这个错误:
You have requested a non-existent service "router".
I'm guessing that i need to inject further services whatsoever, but i don't know what to inject, so what do i need to further add so my Controller can work with all services? 我猜想我需要注入任何其他服务,但是我不知道要注入什么,因此我需要进一步添加什么以便我的Controller可以使用所有服务?
My MailController looks like this 我的MailController看起来像这样
namespace Company\Project\Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\Container;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Doctrine\ORM\EntityManager;
class MailController extends Controller{
public function __construct() {
$this->setContainer(new Container());
}
//Code for mailstuff
}
You're creating a new container rather than injecting the built container so it has no services. 您正在创建一个新容器,而不是注入已构建的容器,因此它没有服务。
To use your controller you need to inject the pre made service container in to your controller through your service like so.. 要使用控制器,您需要像这样通过服务将预制的服务容器注入到控制器中。
services:
mail_controller:
class: Company\Project\Bundle\Controller\MailController
calls:
- [ setContainer, [ @service_container ]]
.. and get rid of the setter in your __construct
. ..并摆脱
__construct
中的二传手。
injecting the whole service container 注入整个服务容器
calls: - [ setContainer, [ @service_container ]]
defeats the purpose of declaring your controller as a service. 无法实现将控制器声明为服务的目的。 Just inject the service(s) you need in your constructor.
只需在构造函数中注入所需的服务即可。 The constructor needs the service handed as an parameter and do not extend
Controller
anymore. 构造函数需要将服务作为参数传递,并且不再扩展
Controller
。
//MailController
use Symfony\Component\Routing\RouterInterface;
class MailController
{
private $router;
public function __construct(RouterInterface $router){
$this->router = $router;
}
//actions
}
Now you need to adjust your services.yml
and extend the service with arguments describing the service you need 现在,您需要调整您的
services.yml
并使用描述所需服务的参数扩展服务
services:
mail_controller:
class: Company\Project\Bundle\Controller\MailController
arguments:
- @router
et voila, only one service needed, only one service injected. 等等,仅需要一项服务,仅注入一项服务。 If you find yourself injecting too many services in one action, chances are your action/controller is not 'thin' enough.
如果您发现自己在一项操作中注入了过多的服务,则很可能是您的操作/控制器不够“瘦”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.