简体   繁体   English

Symfony 2称不存在的服务“路由器”

[英]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.

相关问题 Symfony 2,ServiceNotFoundException,不存在的服务“ templating.engine.php” - Symfony 2, ServiceNotFoundException, non-existent service “templating.engine.php” 如何在Symfony 4中配置不存在的服务? - How can I configure a non-existent service in Symfony 4? 自symfony 3.3以来不存在的服务错误 - Non-existent service error since symfony 3.3 Symfony 4.1:你请求了一个不存在的服务“主义” - Symfony 4.1 : You have requested a non-existent service "doctrine" Symfony2错误:服务“模板”依赖于不存在的服务“ templating.globals” - Symfony2 error: The service “templating” has a dependency on a non-existent service “templating.globals” Drupal 8:您请求的服务不存在“ router.route_provider.old” - Drupal 8: You have requested a non-existent service “router.route_provider.old” Symfony2:您请求的服务“ jms_aop.pointcut_container”不存在。 - Symfony2: You have requested a non-existent service “jms_aop.pointcut_container”. Symfony3您已请求不存在的服务“学说”。 MongoDB FOSUserBundle - Symfony3 You have requested a non-existent service “doctrine”. MongoDB FOSUserBundle Symfony:服务...依赖于不存在的参数kernel.secret - Symfony: The service … has a dependency on a non-existent parameter kernel.secret 您已请求一个不存在的服务“ file.uploader”-Symfony3 - You have requested a non-existent service “file.uploader” - Symfony3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM