简体   繁体   English

带参数的树枝扩展

[英]twig extension with arguments

I am trying to see to make a twig extension but I hang on @security.context injection because it returns me an error for me to add the argument in the constructor? 我正在尝试进行细枝扩展,但是我挂起@ security.context注入,因为它为我返回了一个错误,要求我在构造函数中添加参数? I'm several people searched the internet but the problem is I can not find the solution is what I do wrong? 我有几个人在上网搜索,但问题是我找不到解决方案是我做错了什么?

The goal is to inject security.context to have access to the current user. 目标是注入security.context以访问当前用户。

<?php
namespace Acme\AppBundle\Twig\Extension;

use Symfony\Component\Security\Core\SecurityContextInterface;

class reserveExtension extends \Twig_Extension
{
    protected $context;

    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    public function getFilters()
    {
        return array(new \Twig_SimpleFilter('reserve_currentUser', array($this, 'reserveCurrentUser')));
    }

    function reserveCurrentUser($entity)
    {
        var_dump($this->context->getToken()->getUser());
    }

    public function getName()
    {
        return 'twig_extension_reserve';
    }
}

Service definition: 服务定义:

services:
reserveExtension:
    class: Acme\AppBundle\Twig\Extension\reserveExtension
    arguments: [@security.context]
    tags:
        - { name: twig.extension }

Error: 错误:

Catchable Fatal Error: Argument 1 passed to Acme\AppBundle\Twig\Extension\ReserveExtension::__construct() must be an instance of Acme\AppBundle\Twig\Extension\SecurityContext, instance of Symfony\Component\Security\Core\SecurityContext given, called in D:\wamp\www\Acme\app\cache\dev\appDevDebugProjectContainer.php on line 432 and defined

Thank you ! 谢谢 !

您的use语句错误,您已经use ...\\SecurityContextInterface ,但是实际代码中包含SecurityContext ,请更改其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM