简体   繁体   中英

Protecting Routes in Laravel 5.3

I am trying to protect my routes in Laravel 5.3. I am using below codes

Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

If I try to browse /profile in logout situation it redirects me to /login route. But I would like to redirect it to / route.

How can I do that ??

change file app\\Middleware\\RedirectIfAuthenticated.php

and edit this line:

return redirect('/login');

On laravel 5.3 it's on Exceptions directory. Go to App\\Exceptions\\Handler.php and on the bottom change the code:

    return redirect()->guest('/');
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
}

Please write this function in this file app\\Middleware\\RedirectIfAuthenticated.php

You can try

Route::group(['middleware'=>'web'],function (){
Route::Auth();
Route::get('/home', 'HomeController@index');});

and change app\\Middleware\\RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM