我不得不问如何将登录用户的名称放入Nette组件(SomethingControl.php)中。显然我不能这么做:
$identity = $this->getUser()->getIdentity();
if ($identity) $this->template->username = $identity->getData()['username'];所以我试着这样做:
$this->template->username = $this->user但这也不起作用。
发布于 2017-01-04 23:35:06
您无法以这种方式获取用户,因为UI\Control不是UI\Presenter的后代。但是Nette\Security\User是在DIC中注册的服务,所以你可以像这样得到它:
class SomethingControl extends \Nette\Application\UI\Control
{
/**
* @var \Nette\Security\User
*/
private $user;
public function __construct(\Nette\Security\User $user)
{
parent::__construct();
$this->user = $user;
}
public function render()
{
bdump($this->user); // getIdentity and username
}
}只需确保您使用的是Component Factory -意思是不要使用new运算符在presenter中创建您的组件。
https://stackoverflow.com/questions/41466756
复制相似问题