不存在的服务 "request_stack"
Non-existent service "request_stack"
我正在编写 Symfony 2.6 应用程序,在尝试将 RequestStack 注入服务时遇到了问题。我想要的是能够从我的服务中获取当前请求,但出现以下异常:
ServiceNotFoundException: The service "host_service" has a dependency on a non-existent service "request_stack".
我的服务
我的服务代码如下所示:
<?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 文件,如下所示:
# MyBundle/Resources/config/services.yml
services:
host_service:
class: MyBundle\DependencyInjection\HostService
arguments: ["@request_stack"]
分机
我还为我的包创建了一个扩展:
<?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');
}
}
控制器
我使用服务的方式是这样的:
$hostService = $this->get('host_service');
$something = $hostService->getSomething();
也试过
我也试过用这样的方法注入它:
protected $request;
public function setRequest(RequestStack $request)
{
$this->request = request;
}
但这也不起作用(同样的例外,是的,我也在 Services.yml 中更改了服务)。
问题
根据 Symfony.com 上的 this article,您不应注入 Request 服务,而应使用 RequestStack。文章做的同样的事情我一直在尝试做,但是还是不行。
我是不是忘记了什么?我是否完全以错误的方式使用这些服务(这是我的第一项服务)?我没看到什么吗?为什么服务 request_stack 不存在?最重要的是:为什么这不起作用?
导致解决方案的信息
我使用的是 Symfony 2.3,而不是 Symfony 2.6。
使用 php app/console container:debug or debug:container for 2.6 检查服务是否存在,也许你使用了错误版本的 symfony
自 2017 年和 Symfony 3.3+以来,这现在非常简单。
1。通过 PSR-4 service autodiscovery
使用自动装配注册服务
# app/config/services.yml
services:
_defaults:
autowire: true
App\:
resource: ../../src/App
2。通过构造函数注入
要求 RequestStack
在服务中
<?php
namespace App;
use Symfony\Component\HttpFoundation\RequestStack;
final class SomeService
{
/**
* @var AnotherService
*/
private $requestStack;
public function __construct(RequestStack $anotherService)
{
$this->requestStack = $requestStack;
}
public function someMethod()
{
$request = $this->requestStack->getCurrentRequest();
// ...
}
}
3。或者在控制器中,要求 Request
作为操作方法参数
<?php
namespace App;
use Symfony\Component\HttpFoundation\Request;
final class SomeController
{
public function someAction(Request $request)
{
$request->...;
}
}
仅此而已。
编码愉快!
我正在编写 Symfony 2.6 应用程序,在尝试将 RequestStack 注入服务时遇到了问题。我想要的是能够从我的服务中获取当前请求,但出现以下异常:
ServiceNotFoundException: The service "host_service" has a dependency on a non-existent service "request_stack".
我的服务 我的服务代码如下所示:
<?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 文件,如下所示:
# MyBundle/Resources/config/services.yml
services:
host_service:
class: MyBundle\DependencyInjection\HostService
arguments: ["@request_stack"]
分机
我还为我的包创建了一个扩展:
<?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');
}
}
控制器
我使用服务的方式是这样的:
$hostService = $this->get('host_service');
$something = $hostService->getSomething();
也试过
我也试过用这样的方法注入它:
protected $request;
public function setRequest(RequestStack $request)
{
$this->request = request;
}
但这也不起作用(同样的例外,是的,我也在 Services.yml 中更改了服务)。
问题
根据 Symfony.com 上的 this article,您不应注入 Request 服务,而应使用 RequestStack。文章做的同样的事情我一直在尝试做,但是还是不行。
我是不是忘记了什么?我是否完全以错误的方式使用这些服务(这是我的第一项服务)?我没看到什么吗?为什么服务 request_stack 不存在?最重要的是:为什么这不起作用?
导致解决方案的信息
我使用的是 Symfony 2.3,而不是 Symfony 2.6。
使用 php app/console container:debug or debug:container for 2.6 检查服务是否存在,也许你使用了错误版本的 symfony
自 2017 年和 Symfony 3.3+以来,这现在非常简单。
1。通过 PSR-4 service autodiscovery
使用自动装配注册服务# app/config/services.yml
services:
_defaults:
autowire: true
App\:
resource: ../../src/App
2。通过构造函数注入
要求RequestStack
在服务中
<?php
namespace App;
use Symfony\Component\HttpFoundation\RequestStack;
final class SomeService
{
/**
* @var AnotherService
*/
private $requestStack;
public function __construct(RequestStack $anotherService)
{
$this->requestStack = $requestStack;
}
public function someMethod()
{
$request = $this->requestStack->getCurrentRequest();
// ...
}
}
3。或者在控制器中,要求 Request
作为操作方法参数
<?php
namespace App;
use Symfony\Component\HttpFoundation\Request;
final class SomeController
{
public function someAction(Request $request)
{
$request->...;
}
}
仅此而已。
编码愉快!