我试图在扩展包中获取当前登录的用户
$user = $this->get("security.context")->getToken()->getUser();
&安培;
$user = $this->get("security.context")->getToken()->getUser();
会返回与string(5) "anon."
$user = $this->getUser(); return `NULL`
以下是我尝试过的代码
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\User;
class ClientController extends Controller
{
public function clientDashboardAction()
{
$user = $this->get("security.context")->getToken()->getUser();
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user);
echo '</pre>';
// returns string(5) "anon."
$user = $this->getUser();
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user);
echo '</pre>';
//returns NULL
return $this->render('AppBundle:Client:clientDashboard.html.twig');
}
}
我的代码有什么问题,请帮我Guyz ........
答案 0 :(得分:0)
我得到的解决方案是我在安全性中使用多个防火墙(管理员端和前端用户端),系统不会在不同防火墙之间共享上下文
http://symfony.com/doc/current/reference/configuration/security.html#firewall-context
# app/config/security.yml
security:
# ...
firewalls:
admin:
# ...
context: my_context
main:
# ...
context: my_context
以上代码解决了我的问题, 谢谢你的支持......