不存在的服务“ request_stack” [英] Non-existent service "request_stack"

查看:96
本文介绍了不存在的服务“ request_stack”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Symfony 2.6应用程序,尝试将RequestStack注入服务时遇到问题。我想要的是能够从服务中获取当前请求,但是出现以下异常:

I am writing a Symfony 2.6 application and I've encountered a problem when trying to inject the RequestStack into a service. What I want is to be able to get the current request from within my service, but I'm getting the following exception:

ServiceNotFoundException: The service "host_service" has a dependency on a non-existent service "request_stack".

我的服务
我的服务代码如下所示:

My service My code for the service looks like this:

<?php

namespace MyBundle\DependencyInjection;

use Symfony\Component\HttpFoundation\RequestStack;

class HostService
{
    protected $request;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function getSomething()
    {
        $request = $this->requestStack->getCurrentRequest();
        // Do stuff with the request stack
        // return something
    }
}

Services.yml

我已经创建了一个services.yml文件,如下所示:

I've created a services.yml file like this:

# MyBundle/Resources/config/services.yml
services:
    host_service:
        class:        MyBundle\DependencyInjection\HostService
        arguments: ["@request_stack"]

扩展

我还为我的捆绑包创建了扩展名:

I have also created an extension for my bundle:

<?php

namespace MyBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class MyExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config')
       );
        $loader->load('services.yml');
    }
} 

控制器

我使用服务的方式是这样的:

The way I'm using the service is like this:

$hostService = $this->get('host_service');
$something = $hostService->getSomething();

也尝试过

我还尝试使用以下方法注入它:

I also tried injecting it using a method like this:

protected $request;

public function setRequest(RequestStack $request)
{
    $this->request = request;
}

但这还是行不通的(相同的例外,是的,我也更改了

But this also did not work (same exception, and yeah I also changed the service in Services.yml).

问题

根据这篇文章,您不应注入该请求服务并改为使用RequestStack。我一直在尝试与文章相同的方法来执行此操作,但仍然无法正常工作。

According to this article on Symfony.com, you should not inject the Request service and use the RequestStack instead. I've been trying to do this in the same matter the article is doing it, but it still won't work.

我忘记了什么吗?我是否完全以错误的方式使用了服务(这是我的第一个服务)?我没看到什么吗?为什么服务request_stack不存在?最重要的是:为什么这行不通?

Am I forgetting something? Am I using the services in the wrong way altogether (this is my first service)? Am I not seeing something? Why does the service request_stack not exist? Most of all: why is this not working?

导致解决方案的信息

我正在使用Symfony 2.3,而不是Symfony 2.6。

I am using Symfony 2.3, not Symfony 2.6.

推荐答案

使用php应用程序/控制台容器:调试或调试: 2.6的容器以检查服务是否存在,也许您使用的是错误版本的symfony

Use php app/console container:debug or debug:container for 2.6 to check if the service exists , and maybe you are using the wrong version of symfony

这篇关于不存在的服务“ request_stack”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆