Php 如何在细枝扩展服务中获取当前用户[symfony2.8]
我正在尝试在我的Php 如何在细枝扩展服务中获取当前用户[symfony2.8],php,symfony,twig,Php,Symfony,Twig,我正在尝试在我的NotificationExtension.php中获取当前用户。但是页面加载速度变得非常慢,我也会遇到以下错误: 错误:在null上调用成员函数getUser() 错误说明无法获取当前用户,但我正在登录 这是我的服务: notification: class: Application\Sonata\UserBundle\Twig\NotificationExtension arguments: ['@doctrine.orm.entity_manager', '@ser
NotificationExtension.php
中获取当前用户。但是页面加载速度变得非常慢,我也会遇到以下错误:
错误:在null上调用成员函数getUser()
错误说明无法获取当前用户,但我正在登录
这是我的服务:
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager', '@service_container', '@security.context']
tags:
- { name: twig.extension }
<?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';
}
}
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager','@security.token_storage']
tags:
- { name: twig.extension }
public function getUser()
{
return $this->tokenStorage->getToken()->getUser();
}
通知扩展名:
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager', '@service_container', '@security.context']
tags:
- { name: twig.extension }
<?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';
}
}
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager','@security.token_storage']
tags:
- { name: twig.extension }
public function getUser()
{
return $this->tokenStorage->getToken()->getUser();
}
获取当前用户:
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager', '@service_container', '@security.context']
tags:
- { name: twig.extension }
<?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';
}
}
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager','@security.token_storage']
tags:
- { name: twig.extension }
public function getUser()
{
return $this->tokenStorage->getToken()->getUser();
}
将服务定义为全局细枝变量:
# app/config/config.yml
twig:
# ...
globals:
user_notification: '@app.user_notification'
服务类别:
// 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;
}
}
最后,对于可以使用此服务的所有模板:
# foo.html.twig
{{ user_notification.unreadMessages|length }}
无论何时在模板中访问全局变量,都将从服务容器请求服务,您可以访问该对象
更多信息您可以这样访问用户名:app.user.username
如果要检查用户是否已登录,可以使用is_grated twig功能
例如:
尝试使用service@security.token\u storage
并访问用户身份:$this->user=($tokenStorage->getToken())$tokenStorage->getToken()->getUser():null
当我将您的行放入公共函数getGlobals()
时,出现以下错误<代码>注意:未定义变量:tokenStorage
我的意思是在服务定义和类中使用$tokenStorage
更改您的SecurityContext$context
。由于通知扩展对象是在请求过程的早期创建的,所以您所拥有的将不起作用,在对用户进行身份验证和加载之前。你需要做的是添加一个函数来获取你的用户数据,然后根据需要从你的小树枝模板调用它。我就是这么做的。我的服务是这样的:参数:['@doctrine.orm.entity_manager','@service_container','@security.token_storage']
并且在我的公共函数uu构造中添加了这个:TokenStorageInterface$tokenStorage
感谢您的回复。但是在userNotification中,这一行$user=$token->getUser()
和此行返回$unreadMessages代码>未定义。是的,您应该完成这些行,我只向您展示了解决用户问题的提示。您只需要插入entity manager服务并进行查询;)非常感谢你的帮助。我是个新手,有时候简单的事情对我来说可能很难,但我每天都在学习。