Symfony2: Inject current user in Service

后端 未结 7 2033
夕颜
夕颜 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:14

    The user is a bad candidate to be a service.

    • First it is a model not a service
    • Second there is service security.context where you can get user from.

    In a twig template you can use app.user. See symfony doc global-template-variables. If you want to show something based on user permissions you can do {{ is_granted('ROLE_USER') }}.

提交回复
热议问题