【发布时间】:2024-05-18 22:20:02
【问题描述】:
我正在使用 auth 中间件并创建和管理保护来控制管理员访问。我在尝试访问管理员路由时遇到了一些问题,我想将与管理员路由关联的未经身份验证的流量重定向到 /admin/login 页面,但不是那样,而是将我重定向到 /login 页面。 我不知道如何在 Authenticate 类中获取与路由相关联的警卫。
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}
这就是代码,我想是这样的:
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if(Auth::guard('admin'))
return route('admin.login);
else
return route(login);
}
}
}
但它不起作用,因为我唯一的参数是 $request。
这些是我的路线...
//Admin Routes
Route::middleware(['auth:admin'])->group(function () {
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/newDoncente', 'AdminController@addDocenteView')->name('newDocente');
//Docentes Routes
Route::get('/docentes', 'Docente\DocenteController@getDocentesView')->name('getDocentesView');
Route::get('/editDocente/{id}', 'Docente\DocenteController@editDocenteView')->name('editDocentesView');
Route::get('/docentesTables', 'Docente\DocenteController@getDocentesDatatables')->name('getDocentesTables');
Route::get('/docente/{id}', 'Docente\DocenteController@getDocenteView')->name('getDocenteView');
谢谢。
【问题讨论】:
-
您可以根据路线放置条件。
-
怎么样?我可以修补它,将 /admin/ 路径添加到所有路由...'因为请求我可以获得路径,但我不知道这是否正确。
标签: php laravel authentication laravel-7 guard