Exclude route from Laravel authentication

After running php artisan make:auth all the necessary routes are in the route.php file, but is it possible to delete it (I want to delete the registration route)?

I currently have

 Route::group(['middleware' => 'web'], function () { Route::auth(); }); 

I know that Route::auth() is a shortcut to add all routes. Should I specify routes myself, and not use a shortcut?

+8
php laravel
source share
5 answers

Unfortunately, you cannot exclude case with the current implementation of Route::auth() .

You will need to specify all routes manually to

 // Authentication Routes... $this->get('login', 'Auth\ AuthController@showLoginForm '); $this->post('login', 'Auth\ AuthController@login '); $this->get('logout', 'Auth\ AuthController@logout '); // Password Reset Routes... $this->get('password/reset/{token?}', 'Auth\ PasswordController@showResetForm '); $this->post('password/email', 'Auth\ PasswordController@sendResetLinkEmail '); $this->post('password/reset', 'Auth\ PasswordController@reset '); 

I think this is a fairly common thing that I want to do, it would be nice if there was an auth parameter to say without registration, maybe you could send a pull request to the project.

+8
source share

I am just YOLO and change this in RegisterController.php

 public function __construct() { $this->middleware('guest'); } 

to that

 public function __construct() { $this->middleware('auth'); } 

This causes the register page to require you to log in to contact it.

This is a hack. But this is a good hack.

EDIT: and just add this to your seeder to make life easier:

  $u1= new App\User; $u1->name = 'Your name'; $u1->email = ' your@mail.com '; $u1->password = bcrypt('yourPassword'); $u1->save(); 
+3
source share

As Mark Davidson said, this is not possible out of the box. But this is how I did it.

Now this may be redundant, but I am passing an array of what I need. If no parameters are passed, default routes are created.

 // Include the authentication and password routes Route::auth(['authentication', 'password']); 
 /** * Register the typical authentication routes for an application. * * @param array $options * @return void */ public function auth(array $options = []) { if ($options) { // Authentication Routes... if (in_array('authentication', $options)) { $this->get('login', 'Auth\ AuthController@showLoginForm '); $this->post('login', 'Auth\ AuthController@login '); $this->get('logout', 'Auth\ AuthController@logout '); } // Registration Routes... if (in_array('registration', $options)) { $this->get('register', 'Auth\ AuthController@showRegistrationForm '); $this->post('register', 'Auth\ AuthController@register '); } // Password Reset Routes... if (in_array('password', $options)) { $this->get('password/reset/{token?}', 'Auth\ PasswordController@showResetForm '); $this->post('password/email', 'Auth\ PasswordController@sendResetLinkEmail '); $this->post('password/reset', 'Auth\ PasswordController@reset '); } } else { // Authentication Routes... $this->get('login', 'Auth\ AuthController@showLoginForm '); $this->post('login', 'Auth\ AuthController@login '); $this->get('logout', 'Auth\ AuthController@logout '); // Registration Routes... $this->get('register', 'Auth\ AuthController@showRegistrationForm '); $this->post('register', 'Auth\ AuthController@register '); // Password Reset Routes... $this->get('password/reset/{token?}', 'Auth\ PasswordController@showResetForm '); $this->post('password/email', 'Auth\ PasswordController@sendResetLinkEmail '); $this->post('password/reset', 'Auth\ PasswordController@reset '); } } 

In your case, you can just pass the boolean as parameter instead of array . If the boolean value is true , then do not load register routes, otherwise load everything.

Hope this helps.

+1
source share

You can add a redirect to the / register route as follows:

 <?php Auth::routes(); // prevent registration, this needs to be after Auth::routes() Route::redirect('register', '/home', 301); 
0
source share

In new releases, you can do it like this (in your web.php file):

 Auth::routes(['register'=>false]); 
0
source share

All Articles