Php 组件中的Nette getUser
我必须问一下如何将登录用户的姓名输入Nette组件(SomethingControl.php)。显然我不能这么做:Php 组件中的Nette getUser,php,nette,Php,Nette,我必须问一下如何将登录用户的姓名输入Nette组件(SomethingControl.php)。显然我不能这么做: $identity = $this->getUser()->getIdentity(); if ($identity) $this->template->username = $identity->getData()['username']; 所以我试过这个: $this->template->username = $this->u
$identity = $this->getUser()->getIdentity();
if ($identity) $this->template->username = $identity->getData()['username'];
所以我试过这个:
$this->template->username = $this->user
但这也不起作用。您无法获得这样的用户,因为
UI\Control
不是的后代。但是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
}
}
请确保您正在使用-means,不要在演示者中使用
new
运算符创建组件。据我所知-是的。它会返回对未定义函数App\MainModule\Components\bdump()的错误调用噢,很抱歉,它是2.4。您可以只使用dump
或\Tracy\Debugger::barDump(…)
。请参阅:。但是它只是一个转储,这样你就可以调试$this->user
中的内容。有更好的方法吗?使用依赖注入是最好的方法。