首页 > 解决方案 > 在 Laravel 5.6 中登录后更改默认重定向

问题描述

我是laravel的初学者。我正在使用 udemy 视频来学习 laravel。我想在登录后更改默认重定向。我将路线更改为

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

并且在Http/Middleware/RedirectIfAuthenticated.php

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

        return $next($request);
    }

但是当我再次登录时,我重定向到/home而不是/admin/home. 我清除了缓存,但它不再起作用了。

标签: phplaravel

解决方案


如果您希望重定向只是未经授权的,那么只需编写

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

如果你想在登录后重定向到“admin/home”,你可以更改

登录控制器

 protected $redirectTo = '/admin/home';

推荐阅读