【发布时间】:2020-06-15 17:19:20
【问题描述】:
当有人在 URL 中点击主页时,我试图重定向到登录页面,但总是会出现类似
的错误路由 [admin/login] 未定义。
许多问题都存在相同的问题,但没有解决问题。
如果直接输入 URL,同样的路由也可以工作,但是从 Authenticate.php 重定向不起作用。
routes/web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
// Admin Routes
// Without auth
Route::group(['prefix' => 'admin', 'namespace' => 'Auth'], function () {
Route::get('/login', 'AdminLoginController@login');
});
Route::group(['prefix' => 'admin', 'namespace' => 'Auth', 'middleware' => 'auth:admin'], function () {
Route::get('/home', 'AdminLoginController@home');
});
Authenticate.php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if ($request->is('admin') || $request->is('admin/*')) {
return route('admin/login');
} else if ($request->is('vendor') || $request->is('vendor/*')) {
return route('vendor/login');
} else {
return route('login');
}
}
}
}
【问题讨论】:
标签: php laravel laravel-6 laravel-6.2