问题
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