Laravel:如何确保用户在注册和登录后进入特定于角色的主视图?
我在laravel-5.8中为一个学院网站创建了一个自定义的多重身份验证系统,在该系统中,每个用户都注册了一个特定的角色:管理员、教员或HOD(系主任) 我正在使用运行后获得的控制器和视图Laravel:如何确保用户在注册和登录后进入特定于角色的主视图?,laravel,laravel-authorization,laravel-5.8,laravel-authentication,Laravel,Laravel Authorization,Laravel 5.8,Laravel Authentication,我在laravel-5.8中为一个学院网站创建了一个自定义的多重身份验证系统,在该系统中,每个用户都注册了一个特定的角色:管理员、教员或HOD(系主任) 我正在使用运行后获得的控制器和视图 php artisan make:auth 注册后,所有用户都将重定向到默认主页,而不考虑其角色。这3个角色作为0、1和2存储在用户表中的列角色下。我修改了默认情况下的用户表 我的登录机制运行良好。用户将重定向到其角色的主页。我是这样做的: LoginController.php 这是login.blade
php artisan make:auth
注册后,所有用户都将重定向到默认主页,而不考虑其角色。这3个角色作为0、1和2存储在用户表中的列角色下。我修改了默认情况下的用户表
我的登录机制运行良好。用户将重定向到其角色的主页。我是这样做的:
LoginController.php
这是login.blade.php发出POST请求时使用的路由
但每次我注册一个新用户时,它都会将用户带到教员主页路径,即使他们是管理员或HOD。以下是注册机制:
RegisterController.php(仅在下面显示相关函数)
我已经尝试通过这样做使其正常工作:将变量重定向到
更改为函数。
//一个想法:将“protected$redirectTo='/home';”替换为
但它不起作用,因为我似乎错过了一些东西
另一个令人不快的事实是,登录后,用户可以访问其他角色的主页。my routes中的中间件('auth')
有助于防止未登录时访问URL,但它不会阻止登录用户访问其他主页
以下是到主页的路线:
Auth::routes();
//-------------------MULTI AUTH SYSTEM------------------------------------------
Route::post('/login/custom', 'auth\LoginController@login')->name('login.custom');
Route::get('/home', 'HomeController@index')->name('home');
route::get('/adminhome', function () {
return view('adminhome');
})->middleware('auth');
route::get('/hodhome', function () {
return view('hodhome');
})->middleware('auth');
//----------------------------------------------------------------------------
默认的welcome.blade.php
也会将登录用户重定向到教员主页:
@if (Route::has('login'))
<div class="top-right links">
@auth
<a href="{{ url('/home') }}">Home</a>
@else
<a href="{{ route('login') }}">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}">Register</a>
@endif
@endauth
</div>
@endif
如何更改此视图文件以将登录用户发送到其角色的主页 一个完整而正确的解决方案是将实体(HOD、Admin、User)分开,并为每个实体(auth:HOD、auth:Admin、auth)配置auth 另一个解决方案是制作3个独立的中间件,并将您的路由与其中一个相应地分组 顺便说一句,
redirectTo()
方法不接受参数。从illible\Foundation\Auth\RedirectsUsers
trait
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
---------------编辑
对于3中间件解决方案,您可以使用命令php artisan make:middleware Admin
,这将创建类文件app/Http/middleware/Admin.php
在handle
方法中,可以执行以下操作
public function handle($request, Closure $next)
{
if(auth()->user() && auth()->user()->role == 2){
return $next($request);
}
return redirect(‘login’)->with(‘error’,’You have not admin access’);
}
对霍德也一样
要在路由中像使用auth
一样使用它,请在$routeMiddleware
中的内核App\Http\kernel.php
中添加中间件
protected $routeMiddleware = [
//....,
//....,
'admin' => \App\Http\Middleware\Admin::class,
];
一个完整而正确的解决方案是分离实体(HOD、Admin、User)并为每个实体(auth:HOD、auth:Admin、auth)配置auth 另一个解决方案是制作3个独立的中间件,并将您的路由与其中一个相应地分组 顺便说一句,
redirectTo()
方法不接受参数。从illible\Foundation\Auth\RedirectsUsers
trait
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
---------------编辑
对于3中间件解决方案,您可以使用命令php artisan make:middleware Admin
,这将创建类文件app/Http/middleware/Admin.php
在handle
方法中,可以执行以下操作
public function handle($request, Closure $next)
{
if(auth()->user() && auth()->user()->role == 2){
return $next($request);
}
return redirect(‘login’)->with(‘error’,’You have not admin access’);
}
对霍德也一样
要在路由中像使用auth
一样使用它,请在$routeMiddleware
中的内核App\Http\kernel.php
中添加中间件
protected $routeMiddleware = [
//....,
//....,
'admin' => \App\Http\Middleware\Admin::class,
];
如何创建您提到的3个独立的中间件
auth
、auth:admin
和auth:hod
?一旦创建,我如何使用它们?太棒了!它工作得很好。用户角色只能查看其页面,而不能查看其他角色的页面。还有一个问题:没有显示错误消息。相反,它将用户重定向到root:welcome页面。有关详细信息,请查看我的问题的EDIT 1部分。当重定向到错误时,您可以使用$error
对象,@if($errors->any()){{$errors->first()}@endif
显示错误。选择在中间仓库中重定向如何创建您提到的3个独立的中间仓库auth
、auth:admin
和auth:hod
?一旦创建,我如何使用它们?太棒了!它工作得很好。用户角色只能查看其页面,而不能查看其他角色的页面。还有一个问题:没有显示错误消息。相反,它将用户重定向到root:welcome页面。有关详细信息,请查看我的问题的EDIT 1部分。当重定向到错误时,您可以使用$error
对象,@if($errors->any()){{$errors->first()}@endif
显示错误。选择要在中间件中重定向的对象
public function handle($request, Closure $next)
{
if(auth()->user() && auth()->user()->role == 2){
return $next($request);
}
return redirect(‘login’)->with(‘error’,’You have not admin access’);
}
protected $routeMiddleware = [
//....,
//....,
'admin' => \App\Http\Middleware\Admin::class,
];