0

如何将 Laravel 7 auth home URL 重定向到仪表板。

我的路由文件 route/web.php

use Illuminate\Support\Facades\Route;    

Auth::routes();
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
Route::get('/', function () {
    return view('/home');
})->middleware('auth');

MyLogin 控制器看起来像这样

namespace App\Http\Controllers\Auth;    
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}
4

2 回答 2

0

在花了几个小时后,我找到了以下解决方案

只需在 app\Providers\RouteServiceProvider.php 中进行更改

public const HOME = '/home';

public const DASHBOARD = '/dashboard';

然后在另一个文件中进行小的更改,即 app\Http\Controllers\Auth\LoginController.php

protected $redirectTo = RouteServiceProvider::HOME;

protected $redirectTo = RouteServiceProvider::DASHBOARD;

在 app\Http\Middleware\RedirectIfAuthenticated.php 中进行最后的更改

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

        return $next($request);
    }

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

        return $next($request);
    }

将您的路线更改为

Route::get('/', function () {
    return redirect('/dashboard');    
})->middleware('auth');

不打开 CMD 并在项目文件夹中运行命令php artisan optmize:clear 并立即通过登录名检查。

于 2020-07-17T08:53:10.793 回答
0

如此简单的解决方案是:

use Illuminate\Support\Facades\Route;    

Auth::routes();
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
Route::get('/', 'HomeController@index');
于 2020-07-17T09:34:32.050 回答