php - Symfony 2.8 : isScopeActive deprecation after update to 2. 8.0 来自 2.7.7

标签 php symfony symfony-2.8

我已经从 2.7.7 更新到 symfony 2.8,我得到了这个弃用:

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

我在 twig 扩展类中使用这个调用:

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
    }

首先我删除了 isScopeActive 检查,但是当我运行 symfony 缓存清除时我得到一个异常:

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

有什么方法可以替代 isScopeActive 检查吗?

谢谢...

最佳答案

只需注入(inject) request_stack 而不是 request 并调用 getCurrentRequest() .

关于php - Symfony 2.8 : isScopeActive deprecation after update to 2. 8.0 来自 2.7.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136957/

相关文章:

用 C/C++ 编写的 PHP 词法和语法分析器

php - 数组值被截断为一位数

php - Symfony2 在 Twig 中显示时最小化查询

php - Symfony 4.4 Auth0如何从应用程序中完全注销用户

php - 在 Symfony 3 中组织模型

mysql - 案例在 Doctrine 查询生成器 where 子句中不起作用

php - 没有主键的 Laravel 模型保存数据

php - 使用 Hotmail smtp 在 PHP 中发送邮件

many-to-many - Symfony 预期必须是 Doctrine\Common\Collections\ArrayCollection 的实例,给出的 Doctrine\ORM\PersistentCollection 的实例

php - Symfony - 在字段约束类中获取实体对象