2015-08-13 84 views
0

我想看到做一個枝條擴展,但我掛在@ security.context注入,因爲它返回給我一個錯誤,我要在構造函數中添加參數?我是幾個人搜索互聯網,但問題是我找不到解決辦法是我做錯了什麼?帶參數的枝條擴展

目標是注入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'; 
    } 
} 

服務定義:

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

錯誤:

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 

謝謝!

回答

1

您的使用說明有誤,您有use ...\SecurityContextInterface,但您在實際代碼中有SecurityContext,請更改其中一個。

+0

非常感謝你我封鎖了一段時間! – user3862097