如何在树枝扩展服务中获取当前用户[symfony2.8]

Sir*_*ius 4 php symfony twig

我试图获取当前user在我的NotificationExtension.php。但是页面加载非常缓慢,并且我也收到此错误:

错误:在null上调用成员函数getUser()

错误消息说不可能获得当前用户,但是我正在登录。

这是我的服务:

notification:
  class: Application\Sonata\UserBundle\Twig\NotificationExtension
  arguments: ['@doctrine.orm.entity_manager', '@service_container', '@security.context']
  tags:
        - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

NotificationExtension:

<?php

namespace Application\Sonata\UserBundle\Twig;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Twig_Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\Security;

class NotificationExtension extends \Twig_Extension
{
    protected $container;
    protected $em;

    public function __construct(EntityManager $em,ContainerInterface $container, SecurityContext $context)
    {
        $this->container = $container;
        $this->em = $em;
        $this->doctrine = $container->get('doctrine');
        $this->context = $context;
    }

    public function getGlobals()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();

        return(array(

            'unreadMessagesCount' => $this->em->getRepository('ApplicationSonataUserBundle:Notif')->findBy(
                    array(
                        'user' => $user,
                        'seen' => true
                    ),
                    array('date' => 'DESC')
                )));
    }

    public function getName()
    {
        return 'notification';
    }
}
Run Code Online (Sandbox Code Playgroud)

加:

服务:

notification:
  class: Application\Sonata\UserBundle\Twig\NotificationExtension
  arguments: ['@doctrine.orm.entity_manager','@security.token_storage']
  tags:
        - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

获取当前用户:

public function getUser()
{
    return $this->tokenStorage->getToken()->getUser();
}
Run Code Online (Sandbox Code Playgroud)

yce*_*uto 5

而是将服务定义为全局Twig变量:

# app/config/config.yml
twig:
    # ...
    globals:
        user_notification: '@app.user_notification'
Run Code Online (Sandbox Code Playgroud)

服务类别:

// src/AppBundle/Twig/Globals/UserNotification.php

class UserNotification
{
    private $tokenStorage;
    // ...

    public function __construct(TokenStorageInterface $tokenStorage, ...)
    {
        $this->tokenStorage = $tokenStorage;
        // ...
    }

    public function getUnreadMessages()
    {
        if (null === $token = $this->tokenStorage->getToken()) {
            return array();
        }

        $user = $token->getUser();

        // $unreadMessages = <DB query for get the unread messages from current user>

        return $unreadMessages;
    }
}
Run Code Online (Sandbox Code Playgroud)

服务定义:

# app/config/config.yml

services:
    app.user_notification:
        class: AppBundle\Twig\Globals\UserNotification
        arguments: ['@security.token_storage', ...]
Run Code Online (Sandbox Code Playgroud)

最后,对于所有模板,您可以使用此服务:

# foo.html.twig

{{ user_notification.unreadMessages|length }}
Run Code Online (Sandbox Code Playgroud)

每当在模板中访问全局变量时,都会从服务容器请求服务,并且您可以访问该对象。


更多信息http://symfony.com/doc/current/templating/global_variables.html