2015-10-17 60 views
4

我已经创建了一个具有安全功能的提供者。在the doc之后,我创建了自己的ExpressionLanguage类并注册了提供者。如何在Symfony中注册表达式语言

namespace AppBundle\ExpressionLanguage; 

use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage; 
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface; 

class ExpressionLanguage extends BaseExpressionLanguage 
{ 
    public function __construct(ParserCacheInterface $parser = null, array $providers = array()) 
    { 
     // prepend the default provider to let users override it easily 
     array_unshift($providers, new AppExpressionLanguageProvider()); 

     parent::__construct($parser, $providers); 
    } 
} 

我正在使用相同的功能lowercasein the doc。但是现在,我没有意识到如何注册ExpressionLanguage类来加载我的Symfony项目。

“小写”周围不存在位置的功能26.

我:

我每次我尝试用注释的自定义函数加载页面时得到这个错误使用Symfony 2.7.5。

+0

你是否能够得到这个工作? – Chausser

+0

不,我发现一些不好的方法,比如替换默认的类,但没有真正的本地。 –

回答

0

我假设你想使用你的自定义函数的安全表达式?在这种情况下,与security.expression_language_provider您注册为一个服务创建的表达式语言提供商,其标记为:

services: 
    app.security_expression_language_provider: 
     class: AppBundle\ExpressionLanguage\AppExpressionLanguageProvider 
     tags: 
      - { name: security.expression_language_provider } 
+0

谢谢Wouter。在尝试使用我自己的ExpressionLanguage类之前,我做了它。同样的错误,'位置1附近不存在函数'小写'。' –

+0

您是否清除了缓存? – scoolnico

+0

是的,我做过,scoolnico。 –

1

标签security.expression_language_provider仅用于语言供应商添加到symfonys安全组件所使用的表达语言,或者更具体在ExpressionVoter中。

FrameworkBundle的@ Security-Annotation使用表达式语言的不同实例,它不知道您创建的语言提供程序。

为了能够在@安全性的注释使用自定义的语言学校,我解决了这个使用下面的编译过程:

<?php 

namespace ApiBundle\DependencyInjection\Compiler; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\Reference; 

/** 
* This compiler pass adds language providers tagged 
* with security.expression_language_provider to the 
* expression language used in the framework extra bundle. 
* 
* This allows to use custom expression language functions 
* in the @Security-Annotation. 
* 
* Symfony\Bundle\FrameworkBundle\DependencyInection\Compiler\AddExpressionLanguageProvidersPass 
* does the same, but only for the security.expression_language 
* which is used in the ExpressionVoter. 
*/ 
class AddExpressionLanguageProvidersPass implements CompilerPassInterface 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function process(ContainerBuilder $container) 
    { 
     if ($container->has('sensio_framework_extra.security.expression_language')) { 
      $definition = $container->findDefinition('sensio_framework_extra.security.expression_language'); 
      foreach ($container->findTaggedServiceIds('security.expression_language_provider') as $id => $attributes) { 
       $definition->addMethodCall('registerProvider', array(new Reference($id))); 
      } 
     } 
    } 
} 

这种方式,由ExpressionVoter和FrameworkBundle使用的表达语言都配置使用相同的语言提供者。