How to register expression language in symfony

I have already created a provider with a security feature . After the document, I created my own ExpressionLanguage class and registered the provider.

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);
    }
}

I use the same function lowercaseas in the document . But now I have no idea how to register the ExpressionLanguage class to load in my Symfony project.

I get this error every time I try to load a page with a custom function in the annotation:

The lower case function does not exist at position 26.

I am using Symfony 2.7.5.

+4
source share
2 answers

security.expression_language_provider , symfonys, , , ExpressionVoter.

@Security-Annotation 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, .

+1

, ? , , security.expression_language_provider:

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

Source: https://habr.com/ru/post/1612035/


All Articles