40

I want to override /login route to /admin/login. In web.php I tried

//Auth::routes(); Route::get('login', ['as' => 'auth.login', 'uses' => 'App\Modules\Admin\Controllers\AdminUserController@loginAdminUser'])->name('login'); 

But it is still showing Laravel default login form. How can I do this?

2
  • Just remember that when you update your laravel application, this will all reset. Commented Apr 13, 2017 at 7:19
  • Read this guide medium.com/@panjeh/… Commented Mar 28, 2019 at 17:55

6 Answers 6

86

For the googlers, here is a full list of routes that are getting generated by Auth::routes(); in Laravel >= 5.4

// Authentication Routes... Route::get('login', [ 'as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm' ]); Route::post('login', [ 'as' => '', 'uses' => 'Auth\LoginController@login' ]); Route::post('logout', [ 'as' => 'logout', 'uses' => 'Auth\LoginController@logout' ]); // Password Reset Routes... Route::post('password/email', [ 'as' => 'password.email', 'uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail' ]); Route::get('password/reset', [ 'as' => 'password.request', 'uses' => 'Auth\ForgotPasswordController@showLinkRequestForm' ]); Route::post('password/reset', [ 'as' => 'password.update', 'uses' => 'Auth\ResetPasswordController@reset' ]); Route::get('password/reset/{token}', [ 'as' => 'password.reset', 'uses' => 'Auth\ResetPasswordController@showResetForm' ]); // Registration Routes... Route::get('register', [ 'as' => 'register', 'uses' => 'Auth\RegisterController@showRegistrationForm' ]); Route::post('register', [ 'as' => '', 'uses' => 'Auth\RegisterController@register' ]); 

php artisan route:list will return

+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ | | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest | | | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest | | | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web | | | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest | | | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest | | | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest | | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest | | | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest | | | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest | +--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ 
Sign up to request clarification or add additional context in comments.

4 Comments

Email verification (5.6+): $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice'); $this->get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify'); $this->get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
In newer versions of Laravel (in my case 5.7) the routes are the same but one change was made. The password reset route POST password/reset got an alias 'password.update'. I think it is not a problem to set the alias also for older version. So let's change your solution and add that alias.
@algorhythm Thanks for the hint. You're right, no one will get hurt by the alias.
5.8.17 GET|HEAD: login, password/reset, password/reset/{token}, register POST: login, logout, password/reset, register, password/email
48

You can also try this one.

// Replace admin with whatever prefix you need Route::group(['prefix' => 'admin'], function () { Auth::routes(); }); 

You can see list of routes by following command.

php artisan route:list

enter image description here

6 Comments

I cant thank you enough, not having this caused me to have to start rewriting all the routes, not only that, but all the LoginController methods to override, and the routes renamed to avoid confusion. I was able to remove all my custom routing with this prefix!
Perfect! Works in 5.7
Works with 5.8 :)
Thank you I have been looking for this answer for the last 2 days
Brilliant! This should be the correct answer.
|
13

Routes for 5.5 LTS (Confirmed) / 5.6 (Confirmed) / 5.7 (?)

Can someone confirm it works with 5.7?

// Authentication Routes... Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::post('login', 'Auth\LoginController@login'); Route::post('logout', 'Auth\LoginController@logout')->name('logout'); // Registration Routes... Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); Route::post('register', 'Auth\RegisterController@register'); // Password Reset Routes... Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); Route::post('password/reset', 'Auth\ResetPasswordController@reset'); 

8 Comments

Thanks. This Route Auth for Laravel 5.6 the same :)
Just went here to confirm that my snippet is still working in 5.6
thanks Lucas! but... where is the file so I can overwrite it?
@gtamborero those are routes, lookup for routes documentation on Laravel
Thanks Lucas. thats the directory -> vendor/laravel/framework/src/Illuminate/Routing/Router.php
|
9

As I was struggling with the same problem I managed to find a good way to override laravel 5.5 routes:

The static function Auth::routes(); :

public static function routes() { static::$app->make('router')->auth(); } 

The auth() function is called here which creates auth routes:

laravel/framework/src/Illuminate/Routing/Router.php

public function auth() { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout')->name('logout'); // Registration Routes... $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register'); // Password Reset Routes... $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); $this->post('password/reset', 'Auth\ResetPasswordController@reset'); } 

You can copy&paste the body of the function right into your web.php and modify them as you wish.

Comments

9

You can find all the login routes of Laravel 5.7. There is something new, e-mail verification. Related documentation is here.

If statements in the code block basically enables/disables auth features. By using helper, you can pass register, reset, verify parameters to Auth::routes(['verify' => true]);. So fix if statements by using config() or just use as you wish.

Wrapping up here!
When you call Auth::routes(), following routes will be registered.

Route::get('login', 'LoginController@showLoginForm')->name('login'); Route::post('login', 'LoginController@login'); Route::post('logout', 'LoginController@logout')->name('logout'); // Registration Routes... if (config('register')) { Route::get('register', 'RegisterController@showRegistrationForm')->name('register'); Route::post('register', 'RegisterController@register'); } // Password Reset Routes... if (config('reset')) { Route::get('password/reset', 'ForgotPasswordController@showLinkRequestForm')->name('password.request'); Route::post('password/email', 'ForgotPasswordController@sendResetLinkEmail')->name('password.email'); Route::get('password/reset/{token}', 'ResetPasswordController@showResetForm')->name('password.reset'); Route::post('password/reset', 'ResetPasswordController@reset')->name('password.update'); } // Email Verification Routes... if (config('verify')) { Route::get('email/verify', 'VerificationController@show')->name('verification.notice'); Route::get('email/verify/{id}', 'VerificationController@verify')->name('verification.verify'); Route::get('email/resend', 'VerificationController@resend')->name('verification.resend'); } 

2 Comments

how can i set name for those modules in my laravel app
basically delete Auth::routes() in your route file or overwrite default Laravel auth routes in your route file. Any further help, you must to give more information about the thing you'd like to achieve.
2

Change this to :

Route::get('/admin/login', ['as' => 'admin.login', 'uses' => 'App\Modules\Admin\Controllers\AdminUserController@loginAdminUser']); Route::get('login', ['as' => 'login', 'uses' => 'App\Modules\Admin\Controllers\AdminUserController@loginAdminUser']); 

name function is the synonyms for as key of array. So no need to add name at the end.

4 Comments

What will happen if user try /login?
It will throw a NotFoundHttpException.
But I want to override this route to '/admin/login'
If u want to use both the route you can add both the route. Check the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.