I am using Laravel project version 5.8.
I have used two middleware Admin
and Author
.
I have created both AdminMiddleware
and AuthorMiddlware
.
I registered both in Kernel.php
. I changed RedirectIfAuthenticated
, loginController
for custom redirect. I have created different route groups.
For admin middleware it will redirect admin.dashboard
and for author middleware it will redirect to author.dashboard
.
After then, it is redirecting to the /home
route as in the default laravel logged in users. I want to redirect admin to admin.dashboard and author to author.dashboard after login.Custom redirect is not working. I viewed my project several times but unable to find the issue.
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');
}
I am new in Laravel. Faced this problem first time and unable to find issues please help.
/home
after login? – Immeytiadmin.dashboard
and author toauthor.dashboard
after login – Md Rashedul Islam