【发布时间】:2017-11-19 07:57:02
【问题描述】:
我想创建两个中间件来重定向经过身份验证的用户,如果是管理员,它将被重定向到后台,否则它将被重定向到简单用户的简单仪表板。
但我只想使用 users 表而不为管理员添加另一个表。
RedirectIfAuthenticated.php
<?php
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
if (Auth::user()->role_id == 1)
{
return redirect('/admin/home');
}
return redirect('/dashboard');
}
return $next($request);
}
}
DashboardController.php
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('authFront.layoutAuthenticatedUser.dashboard');
}
}
web.php
Route::get('/us-admin', function () { return redirect('/admin/home'); })->name('admin.dashboard');
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('auth.login');
$this->post('login', 'Auth\LoginController@login')->name('auth.login');
$this->post('register', 'Auth\RegisterController@register')->name('auth.register');
$this->post('logout', 'Auth\LoginController@logout')->name('auth.logout');
// Change Password Routes...
$this->get('change_password', 'Auth\ChangePasswordController@showChangePasswordForm')->name('auth.change_password');
$this->patch('change_password', 'Auth\ChangePasswordController@changePassword')->name('auth.change_password');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('auth.password.reset');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('auth.password.reset');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('auth.password.reset');
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('/home', 'HomeController@index');
});
Route::get('/dashboard', 'DashboardController@index');
【问题讨论】:
-
简单地称呼你的用户并不会为你赢得任何朋友:) 说真的,尽管你到目前为止除了一个好主意之外还有什么?你被困在哪里了?您面临什么问题?
-
谢谢@Dale,我已经为管理员提供了一个后台,现在我希望如果用户想通过我的网站前端进行身份验证,它将被重定向到一个带有个人资料和其他我不希望用户访问管理员后台的元素。
-
只做列来存储用户角色(管理员、超级管理员、用户)
-
@HoàngĐăng 已经搞定了,问题是中间件测试不行。
标签: php laravel authentication laravel-5 laravel-middleware