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();
I suppose you want to restrict access to some pages for guests and only the admin can register a guest. You can achieve it by adding your own middleware on kernel.php file like below:
protected $routeMiddleware = [
'authenticated' => \App\Http\Middleware\AuthenticatedMiddleware::class
];
After creating the middleware you have to use it so you can go on web.php file where your routes are and add it to the route you want to restrict like below:
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');
This way registration is restricted to guests but the admin can still access the page if he ever want to register some other admin!
Don't forget to replace the Auth::routes(); with the detailed list as below:
// 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')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');
// 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');