Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jorqensen's avatar

Modifying the Laravel 5 authentication trait

I've been trying to figure out the best way of handling the login/register in Laravel 5, using the built in authentication system seems to be the way to go. However, I'm a bit confused about a few things.

First of all, the AuthController I can set my protected $redirectPath to something, that works fine. What I am trying to do is basically get the same functionality but with the following routes:

domain.tld/register
domain.tld/login

I tried setting the protected $loginPath but that didn't seem to quite work out, even though the method is in the trait. Maybe I'm missing something here?

Would it be better to remove my Route::controllers from my routes.php and just create the Authentication myself to get it working as I'd like? That way, I can also make sure i use explicit routing.

Cheers.

0 likes
10 replies
graham's avatar

If all you're trying to do is change the path for the login/register page how about something like this in your routes.php:

get('/register', array('as' => 'register', 'uses' => 'Auth\AuthController@getRegister'));
post('/register', array('as' => 'register', 'uses' => 'Auth\AuthController@postRegister'));
get('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@getLogin'));
post('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@postLogin'));

That's what I do but if anyone can suggest a cleaner method, I'm all ears.

2 likes
jorqensen's avatar

Yea I was thinking that as well, however setting that many routes seemed like a lazy solution. Was wondering if there was any cleaner way to do this. Until then, I would probably do this as well. Would still like to hear other options if there's any!

Snapey's avatar

Does it really matter that its /auth/login and not just /login ?

Sorry, just playing devil's advocate.

I assume protected $loginpath is for redirection to the login page when a guest attempts to reach a secured resource.

kfirba's avatar
kfirba
Best Answer
Level 50

@Graham @Moofy There is a little better way:

Route::controller('/', 'Auth\AuthController');

Now in the controller you simply need the methods:

  • getRegister
  • postRegister
  • getLogin
  • postLogin
4 likes
graham's avatar

@kfirba you are a legend, thank you. I suspected there was a tidier way to do it.

jorqensen's avatar

@kfirba There is probably no better way than this, only 1 route and everything works as expected. Marked as correct answer, thank you :)

bbloom's avatar

Here's a contrarian opinion: fork the trait. While you're at it, fork the password trait too. I did. Go for it.

amt925's avatar

@bbloom could you share how you did so? I'm intrigued as to how you did it.

Please or to participate in this conversation.