Symfony 2.4.2:您无法创建非活动范围的服务(“请求”)(“请求”)

时间:2014-02-19 20:18:07

标签: php symfony

我刚从2.2.2更新了一个项目到Symfony 2.4.2。升级后,分析器会导致在每个页面底部内嵌显示的异常:

'Symfony \ Component \ DependencyInjection \ Exception \ InactiveScopeException',消息'您无法创建非活动范围的服务(“请求”)(“请求”)。

( ! ) Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\InactiveScopeException' with message 'You cannot create a service ("request") of an inactive scope ("request").' in /var/www/pm/app/cache/dev/appDevDebugProjectContainer.php on line 3776
( ! ) Symfony\Component\DependencyInjection\Exception\InactiveScopeException: You cannot create a service ("request") of an inactive scope ("request"). in /var/www/pm/app/cache/dev/appDevDebugProjectContainer.php on line 3776
Call Stack
#   Time    Memory  Function    Location
1   0.0029  239928  {main}( )   ../app_dev.php:0
2   4.0354  53462264    Symfony\Component\HttpKernel\Kernel->terminate( )   ../app_dev.php:18
3   4.0355  53462360    Symfony\Component\HttpKernel\HttpKernel->terminate( )   ../bootstrap.php.cache:2283
4   4.0371  53473320    Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher->dispatch( )    ../bootstrap.php.cache:2894
5   4.0377  53477632    Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch( )    ../TraceableEventDispatcher.php:138
6   4.0377  53477632    Symfony\Component\EventDispatcher\EventDispatcher->dispatch( )  ../classes.php:1831
7   4.0377  53477696    Symfony\Component\EventDispatcher\EventDispatcher->doDispatch( )    ../classes.php:1667
8   4.0377  53478048    call_user_func ( )  ../classes.php:1734
9   4.0377  53478088    Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher->Symfony\Component\HttpKernel\Debug\{closure}( )    ../classes.php:1734
10  4.0385  53482400    call_user_func ( )  ../TraceableEventDispatcher.php:388
11  4.0385  53482440    Symfony\Component\HttpKernel\EventListener\ProfilerListener->onKernelTerminate( )   ../TraceableEventDispatcher.php:388
12  4.0387  53486304    Symfony\Component\HttpKernel\Profiler\Profiler->saveProfile( )  ../ProfilerListener.php:136
13  4.0388  53486480    Symfony\Component\HttpKernel\DataCollector\EventDataCollector->lateCollect( )   ../Profiler.php:115
14  4.0388  53486544    Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher->getNotCalledListeners( )   ../EventDataCollector.php:48
15  4.0388  53486752    Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher->getListeners( )    ../TraceableEventDispatcher.php:166
16  4.0388  53486848    Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->getListeners( )    ../TraceableEventDispatcher.php:106
17  4.0403  53497920    Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->lazyLoad( )    ../classes.php:1807
18  4.0416  53520352    Symfony\Component\DependencyInjection\Container->get( ) ../classes.php:1842

似乎错误是在探查器侦听内核终止时引起的,但我无法解决问题。

有什么问题?

修改

请求注入的服务示例:

services:
    property.value.form.resolver.number:
        class: Frisbee\ProductBundle\Form\Resolver\PropertyTypeFormResolverNumber
        arguments: [@form.factory, @request]
        scope: request

1 个答案:

答案 0 :(得分:1)

请尝试使用以下方法之一来处理该异常:

  • 删除scope: request并将arguments: [@form.factory, @request]更改为arguments: [@form.factory, @request=]

  • 使用自Symfony 2.3以来可用的同步服务,其中synchronized: true需要添加到类的服务配置和setter函数中。请求由Symfony DI在范围内可用时设置。参考(http://symfony.com/doc/current/cookbook/service_container/scopes.html):

services:     
    property.value.form.resolver.number:
        class: Frisbee\ProductBundle\Form\Resolver\PropertyTypeFormResolverNumber
        arguments: [@form.factory]
        synchronized: true
        calls:
            - [setRequest, ["@?request="]]
    /**
     * @param Request $request
    */
    public function setRequest(Request $request = null)
    {
        if ($request !== null) {
            $this->request = $request;
        }
    }
  • lazy: true添加到配置中也可以提供帮助:
services:
    property.value.form.resolver.number:
        class: Frisbee\ProductBundle\Form\Resolver\PropertyTypeFormResolverNumber
        arguments: [@form.factory, @request]
        scope: request
        lazy: true