php – 如何在twig扩展服务中获取当前用户[symfony2.8]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何在twig扩展服务中获取当前用户[symfony2.8],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2636字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试在NotificationExtension.php中获取当前用户.但页面加载速度很慢,我也收到此错误:
Error: Call to a member function getUser() on null
错误说不可能获得当前用户,但我正在登录.
这是我的服务:
notification:
class: Application\Sonata\UserBundle\Twig\NotificationExtension
arguments: ['@doctrine.orm.entity_manager', '@service_container', '@security.context']
tags:
- { name: twig.extension }
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';
}
}
加:
服务:
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();
}
解决方法:
将服务定义为全局Twig变量:
# 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;
}
}
服务定义:
# app/config/config.yml
services:
app.user_notification:
class: AppBundle\Twig\Globals\UserNotification
arguments: ['@security.token_storage', ...]
最后,对于所有模板,您都可以使用此服务:
# foo.html.twig
{{ user_notification.unreadMessages|length }}
每当在模板中访问全局变量时,将从服务容器请求服务,并且您可以访问该对象.
更多信息http://symfony.com/doc/current/templating/global_variables.html
内容总结
以上是互联网集市为您收集整理的php – 如何在twig扩展服务中获取当前用户[symfony2.8]全部内容,希望文章能够帮你解决php – 如何在twig扩展服务中获取当前用户[symfony2.8]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。