Search code examples
phplaravelmiddleware

Laravel Redirect to an specific URL


I am using laravel 5.8 and i want a middleware or some technique that can stops unprivileged user to visit any other URL except an specific URL.

When unprivileged user visit a url, he/she should immediately be redirected to a specific allowed page.

Route::get('/home', 'HomeController@index')->name('home')->middleware(['verified',...]);

Solution

  • In you routes file you should run all routes where only authenticated user should have access through the auth middlware.

    Route::group(['middleware' => ['auth']], function() {
        Route::get('/home', 'HomeController@index')->name('home');
        // more routes
    });
    

    To specify where the user should be redirected to you can open the middleware at app/Http/Middlware/Authentication.php and check the method

    protected function redirectTo($request)