I am developing a middleware in order to handle redirect for guests to protected areas. I have 2 login pages, one for frontend and another one for backend and want the guests redirected to each login page depending on where they enter. I mean:
- If the guest enters a route like
/admin/dashboard
they must be redirected to/admin/login
. - If the guest enters route
/messages
they must be redirected to/login
.
This is my routes.php
Route::get('/', function () {
return view('inicio');
});
Route::group(['as'=>'clientRoutes', 'middleware'=>'loginSelector'], function(){
Route::get('/polizas', function () {
return view('cliente.polizas');
});
});
Route::group(['as'=>'adminRoutes','prefix'=>'admin', 'middleware'=>'loginSelector'], function(){
Route::get('/inicio', function () {
return view('admin.inicio');
});
Route::get('/lista', function () {
return view('admin.lista');
});
});
Route::get('/admin/login', function () {
return view('admin.login');
});
In the middleware I was trying to get group name of the route, but cant find the way to get i, something like this:
namespace App\Http\Middleware;
use Closure;
class CustomAuthRedirectMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$grupoRoute = $request->route()->getGroup();
if($grupoRoute == "adminRoutes"){
return redirect('/admin/login');
}else{
return redirect('/login');
}
return $next($request);
}
}
If is possible how can I take the group name for doing the redirection? Or if it is impossible how can achieve this redirect?