I'm making my first component and am trying to get the id of the logged-in user and request some data from the model based on that user id.

If I search around this would return the current user:

use Joomla\CMS\Factory;
$user = Factory::getUser();

But $user returns as an empty object:

Joomla\CMS\User\User Object ( [isRoot:protected] => [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [block] => [sendEmail] => 0 [registerDate] => [lastvisitDate] => [activation] => [para.......

I'm currently doing this in my view.html.php right before $this->items = $this->get('Items'); is called. Could it have something to do with the place where im caling getUser()?


You are doing it right but your user object is not empty.

[id] => 0 because the current user is not logged in.

Try to login and do something like:

use Joomla\CMS\Factory;

$user = Factory::getUser();
if ($user->id > 0)
   echo 'Hello ' . $user->name;
  • Well.. I hadn't realized that being logged in at the back end doesn't mean you are logged in on the frontend aswell
    – Merijn dk
    Sep 4 '19 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.