2017-11-22 1 views
0

symfonyの拡張機能でdoctrineを呼び出してログインする必要があります。symfonyは現在のログインユーザを取得するために複数の引数を拡張機能に追加します

これは私のサービスYML私の場合は

twig.extension.app: 
    class: AppBundle\Twig\AppExtension 
    arguments: 
     $doctrine : '@doctrine' 
     $context : '@security.context' 
    tags: 
     - { name: twig.extension } 


twig.extension.auth: 
    class: AppBundle\Twig\AuthExtension 
    arguments: 
     $doctrine : '@doctrine' 
     $context : '@security.context' 
    tags: 
     - { name: twig.extension } 

ですが、私はそれは2つの拡張であることをdevide。

が、これは私の拡張機能である:

<?php 

namespace AppBundle\Twig; 

use Symfony\Bridge\Doctrine\RegistryInterface; 
use Symfony\Component\Security\Core\Security; 

class AuthExtension extends \Twig_Extension 
{ 
    protected $doctrine; 
    protected $context; 

    public function __construct(RegistryInterface $doctrine, Security $context) 
    { 
     $this->doctrine = $doctrine; 
     $this->context = $context; 
    } 
} 

が、私はエラーだ:私は教義を必要とし、また、現在のユーザーでログインした状態で、

[Symfony\Component\DependencyInjection\Exception\AutowiringFailedException]                               
    Cannot autowire service "AppBundle\Twig\AppExtension": argument "$context" of method "__construct()" references class "Symfony\Component\Security\Core\Security" but no such service exists. It cannot be auto-registered because it is from a different root namespace. 

どのようにこれを解決するの?

+0

ログインしたユーザーにsecurity.token_storageを使用する前に、security.contextを見たことがありません。セキュリティバンドルでフルフレームワークを使用していますか? – Cerad

答えて

0

Twig Extensionをお持ちの場合は、services.ymlに追加する必要があります。これはサンプルです。

twig.json_decode: 
    class: DashboardBundle\Twig\Extension\JsonDecode 
    tags: 
     - { name: twig.extension } 

他のオブジェクトが必要な場合は、オブジェクトに送信する必要があります。

twig.json_decode: 
    class: DashboardBundle\Twig\Extension\JsonDecode 
    arguments: [ "@doctrine_object_name", "@security_object_name" ] 
    tags: 
     - { name: twig.extension } 

別の方法では、service_containerオブジェクトを送信できます。

関連する問題