【发布时间】:2020-11-18 06:19:53
【问题描述】:
我想将登录的用户重定向到'/' 路由(比如example.com,后面没有任何东西)
这行得通:
Route::get('/', Home::class)->name('home');
class Home extends Controller
{
public function __invoke()
{
if(Auth::check()) {
return view('dashboard');
}
else {
return view('welcome');
}
}
}
但是现在我需要在这个路由/控制器中添加中间件。
文档建议将 $this->middleware(['auth', 'verified']) 添加到我的 Home 控制器中的 __constructor。
这不起作用,因为它还会影响来宾的视图 (return view('welcome');)
我也试过了:
Route::get('/', [Home::class, 'index'])->name('home');
class Home extends Controller
{
public function __construct()
{
$this->middleware(['auth', 'verified'])->only('dashboard');
}
public function index()
{
if(Auth::check()) {
$this->dashboard();
}
else {
$this->welcome();
}
}
public function welcome()
{
return view('welcome');
}
public function dashboard()
{
return view('dashboard');
}
}
但这也不起作用。有什么想法吗?
【问题讨论】:
-
要使用
Auth,需要中间件auth。 -
为什么不能是两条不同的路线?
-
@lagbox 当您转到
facebook.com时,您会看到一个登录/注册表单。登录后,您将被重定向到相同的 URL:facebook.com。我想在我的应用程序中使用同样的东西。
标签: laravel routes laravel-fortify