4

I have to ask how to get name of logged user into Nette component (SomethingControl.php). Apparently I can't just do this:

$identity = $this->getUser()->getIdentity();
if ($identity) $this->template->username = $identity->getData()['username'];

So I've tried this:

$this->template->username = $this->user

but that doesn't work either.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Muhaha
  • 61
  • 5

1 Answers1

7

You cannot get user like this, because UI\Control is not descendant of UI\Presenter. But Nette\Security\User is service registered in DIC so you can get it like this:

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
    }

}

Just make sure that you are using Component Factory - means do not create your component in the presenter using new operator.

mrtnzlml
  • 109
  • 1
  • 8