I am trying to disable the register route on my application which is running in Laravel 5.4.
In my routes file, I have only the
Auth::routes();
Since Laravel 5.7, a new $options parameter is introduced to the Auth::routes() method; through which you can pass an array to control the generation of the required routes for user-authentication (valid entries can be chosen from the 'register', 'reset', or 'verify' string literals).
Auth::routes(['register' => false]);
Add this two method to app\Http\Controllers\Auth\RegisterController.php
public function showRegistrationForm(){
return redirect('login');
}
public function register(){
}
Just overwrite your auth showRegistrationForm() method ( place this code inside your Auth/RegisterController )
public function showRegistrationForm(){
return redirect()->route('login');
}
I'm just redirecting my register route to the login route. Here you don't need to append or remove any other code
You could try this.
Route::match(['get', 'post'], 'register', function(){
return redirect('/');
});
Add those routes just below the Auth::routes() to override the default registration routes. Any request to the /register route will redirect to the baseUrl.
This is deceptively easy! You just need to override two methods in your app/Http/Controllers/Auth/RegisterController.php Class. See below which will prevent the form from being displayed and most importantly block direct POST requests to your application for registrations..
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
return redirect('login');
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
abort(404);
}
On my Laravel 5.6 project this method didn't work:
Auth::routes(['register' => false]);
So I had to use the following method:
Route::match(['get', 'post'], 'register', function () {
return abort(403, 'Forbidden');
})->name('register');