$site->user(get('username'))->exists() doesn't work

Hey, I have a registration form and I’m checking if a user exists before submitting the update function, but for some reason it doesn’t work, saying “Call to a member function exists() on null”, even tho the POST method does get the username.

Any explanation?

$site()->user() return the User object if it exists, otherwise it returns null. Just remove the exists() method.

This could be a bug or just some incomplete documentation:

The documentation of $user->exists() states that you can check, if a user exists, by the expression you used:


The documentation of $site->user() is not quite precise about the return value, when searching by a username. It just says:

Fetching any user by username
<?php echo $site->user('bastian')->firstname() ?>

But the code of kirby/core/site.php clearly states, that null is returned, if the user does not exist. But this does not work with the $user->exists() method.

@texnixe, @lukasbestle : Do you know, which implementation is the “right” one? $site->user('some_username') returning null if the user does not exists – which makes the $user->exists() method irrelevant. Or should the constructor of the User class return a new object without a user file to make the exists() method work?
There is also this issue on github: https://github.com/getkirby/kirby/issues/458, but I think, that exact issue does not exist any more in the current code.

1 Like

I didn’t even know that the docs mentioned the exists() method on the User object. I’ve been trusting on null for non-existing users since I’ve learned Kirby.

Yeah, the method exists, but @jbeyerstedt is right, it doesn’t make sense. There is already quite an old issue on GitHub.

If that’s the case I’m going to use $user->exists === null, because it seems to work.

Just to give you an alternative, this is my preference:

if (site()->user('pedro')) {
  // It exists...
} else {
  // Doesn't exists...

Thanks, will use this since it’s cleaner :slight_smile:

1 Like