Symfony 2.8: isScopeActive deprecation after update to 2.8.0 from 2.7.7

左心房为你撑大大i 提交于 2019-12-12 08:09:15

问题


I've updated to symfony 2.8 from 2.7.7 and i get this deprecation:

The Symfony\Component\DependencyInjection\Container::isScopeActive method is deprecated since version 2.8 and will be removed in 3.0.

I use this call in a twig extension class:

class TemplateHelper extends \Twig_Extension {

    private $request;

    private $container;


    /**
     * constructor
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container){
        $this->container = $container;

        if( $this->container->isScopeActive('request') ){
            $this->request = $this->container->get('request');
        }
    }
    //...functions
    }

Firstly i delete the isScopeActive check, but i get an exception when i run the symfony cache clear:

[Symfony\Component\DependencyInjection\Exception\InactiveScopeException] You cannot create a service ("request") of an inactive scope ("request").

Is there any way to replace the isScopeActive check?

Thanks...


回答1:


Simply inject request_stack instead of request and call getCurrentRequest().



来源:https://stackoverflow.com/questions/34136957/symfony-2-8-isscopeactive-deprecation-after-update-to-2-8-0-from-2-7-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!