Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何正确启用细枝';s在Symfony2中的沙盒扩展?_Php_Symfony_Twig_Sandbox - Fatal编程技术网

Php 如何正确启用细枝';s在Symfony2中的沙盒扩展?

Php 如何正确启用细枝';s在Symfony2中的沙盒扩展?,php,symfony,twig,sandbox,Php,Symfony,Twig,Sandbox,在Symfony2中,默认情况下会禁用某些细枝模块。其中一个是调试扩展,它添加了{%debug%}标记(在开发环境中很有用) 要启用它,没有什么困难,您可以将此服务添加到配置中: debug.twig.extension: class: Twig_Extensions_Extension_Debug tags: - { name: 'twig.extension' } 但是如何启用{%sandbox%}标记呢 我的问题是扩展的构造函数采用安全策略: public

在Symfony2中,默认情况下会禁用某些细枝模块。其中一个是调试扩展,它添加了
{%debug%}
标记(在开发环境中很有用)

要启用它,没有什么困难,您可以将此服务添加到配置中:

  debug.twig.extension:
    class: Twig_Extensions_Extension_Debug
    tags:
      - { name: 'twig.extension' }
但是如何启用
{%sandbox%}
标记呢

我的问题是扩展的构造函数采用安全策略:

public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
{
    $this->policy            = $policy;
    $this->sandboxedGlobally = $sandboxed;
}
通过阅读,我看到了本机实现的方法(没有Symfony2):

在使用沙箱之前,我可以在服务中做类似的事情,但这并不像我们习惯的依赖注入那样清晰


是否有更好/正确的方法在Symfony2中启用twig的沙盒扩展?

为什么不创建安全策略的专用服务:

parameters:
    twig.sandbox.tags:
        - if
    twig.sandbox.filters:
        - upper
    twig.sandbox.methods:
        Article: [getTitle, getBody]
    twig.sandbox.properties:
        Article: [title, body]
    twig.sandbox.functions:
        - range

twig.sandbox.policy:
    class: Twig_Sandbox_SecurityPolicy
    arguments:
        - %twig.sandbox.tags%
        - %twig.sandbox.filters%
        - %twig.sandbox.methods%
        - %twig.sandbox.properties%
        - %twig.sandbox.functions%
    public: false
然后,您可以将此服务注入
twig.sandbox.extension
服务:

twig.sandbox.extension:
    class: Twig_Extension_Sandbox
    arguments:
        - @twig.sandbox.policy
    tags:
        - { name: twig.extension }
完成了。将
twig.sandbox.policy
private标记为private可确保使用容器无法访问它(它仍然可以注入其他服务,但我认为这不是问题)


免责声明:我还没有测试过它,在它真正工作之前可能需要一些调整,所以不要复制粘贴

嗨,你能提供更多关于这个答案的细节吗?我们在哪里编写“twig.sandbox.policy”和“twig.sandbox.extension”配置?谢谢这应该在
services.yml
配置中完成(在本例中)。
twig.sandbox.extension:
    class: Twig_Extension_Sandbox
    arguments:
        - @twig.sandbox.policy
    tags:
        - { name: twig.extension }