我正在使用Laravel项目5.8版。
我使用了两个中间件Admin
和Author
。
我已经创建了AdminMiddleware
和AuthorMiddlware
。
我都在Kernel.php
中注册。我更改了RedirectIfAuthenticated
,loginController
进行自定义重定向。我创建了不同的路由组。
对于管理员中间件,它将重定向admin.dashboard
,对于作者中间件,它将重定向到author.dashboard
。
然后,它将重定向到/home
路由,就像默认的laravel登录用户一样。 我想在登录后将admin重定向到admin.dashboard,将作者重定向到author.dashboard 。自定义重定向不起作用。我多次查看了我的项目,但找不到问题。
loginController.php
// protected $redirectTo = '/home';
public function __construct(){
if(Auth::check() && Auth::user()->role->id==1) {
return redirect()->route('author.dashboard');
} else if (Auth::check() && Auth::user()->role->id==2) {
return redirect()->route('admin.dashboard');
}else {
$this->middleware('guest')->except('logout');
}
}
RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null){
if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
return redirect()->route('author.dashboard');
}else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
return redirect()->route('admin.dashboard');;
} else {
return $next($request);
}
}
AuthorMiddleware
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::user()->role->id==1){
return $next($request);
} else {
return redirect()->route('/login');
}
}
AdminMiddleware
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::user()->role->id==2){
return $next($request);
} else {
return redirect()->route('/login');
}
}
Web.php
Route::group(['as'=>'author.','prefix'=>'author','namespace'=>'Author','middleware' => 'author'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});
Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware' => 'admin'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});
Author / DashobardController
public function index()
{
return view('author.dashboard');
}
Admin / DashobardController
public function index()
{
return view('admin.dashboard');
}
我是Laravel的新朋友。第一次遇到此问题,无法找到问题,请提供帮助。
答案 0 :(得分:3)
您需要将以下几行编辑到LoginController.php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
protected function authenticated(Request $request, $user)
{
if(Auth::check() && Auth::user()->role->id==1) {
return redirect()->route('author.dashboard');
} else if (Auth::check() && Auth::user()->role->id==2) {
return redirect()->route('admin.dashboard');
}
return redirect('/home');
}
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
答案 1 :(得分:-1)
更改
RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null){
if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
return '/author/dashboard';
}else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
return 'admin/dashboard';
} else {
return $next($request);
}
}
或
public function handle($request, Closure $next, $guard = null){
if (Auth::user()->role->id==1) {
return '/author/dashboard';
}else if (Auth::user()->role->id==2) {
return 'admin/dashboard';
} else {
return $next($request);
}
}
Logincontoller.php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
protected function authenticated(Request $request, $user)
{
if(Auth::check() && Auth::user()->role->id==1) {
return 'author/dashboard';
} else if (Auth::check() && Auth::user()->role->id==2) {
return 'admin/dashboard';
}
return redirect('/home');
}
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}