我想創建兩個中間件來重定向經過身份驗證的用戶,如果它是管理員,它將被重定向到後臺,否則它將被重定向到簡單用戶的簡單儀表板。Multi Auth Laravel 5.4僅使用兩個中間件
但我想只使用用戶表而不添加管理員的其他表。
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\[email protected]')->name('auth.login');
$this->post('login', 'Auth\[email protected]')->name('auth.login');
$this->post('register', 'Auth\[email protected]')->name('auth.register');
$this->post('logout', 'Auth\[email protected]')->name('auth.logout');
// Change Password Routes...
$this->get('change_password', 'Auth\[email protected]')->name('auth.change_password');
$this->patch('change_password', 'Auth\[email protected]')->name('auth.change_password');
// Password Reset Routes...
$this->get('password/reset', 'Auth\[email protected]')->name('auth.password.reset');
$this->post('password/email', 'Auth\[email protected]')->name('auth.password.reset');
$this->get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset');
$this->post('password/reset', 'Auth\[email protected]')->name('auth.password.reset');
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function() {
Route::get('/home', 'Home[email protected]');
});
Route::get('/dashboard', '[email protected]');
主叫用戶簡單是不會爲你贏得任何朋友:)說真的,儘管除了一個好主意,你還有什麼其他的東西?你卡在哪裏?你面臨什麼問題? – Dale
謝謝@戴爾,我有一個後臺辦公室已經存在的管理員,現在我想,如果用戶想通過我的網站的前端進行身份驗證,它將被重定向到一個小的後臺配置文件和其他元素我不希望用戶訪問管理員的後臺。 –
只是讓列存儲用戶角色(管理員,超級管理員,用戶) –