Symfony2: Inject current user in Service

后端 未结 7 2031
夕颜
夕颜 2020-12-25 10:37

I am trying to inject the currently logged in user into a service. My goal is to extend some twig functionality to output it based on user preferences. In this example I wan

7条回答
  •  清歌不尽
    2020-12-25 11:31

    services.yml

    my_service:
        class: ...
        arguments:
            - "@=service('security.token_storage').getToken().getUser()"
    

    Service.php

    protected $currentUser;
    
    public function __construct($user)
    {
        $this->currentUser = $user;
    }
    

    http://symfony.com/doc/current/book/service_container.html#using-the-expression-language

提交回复
热议问题