I have Middlenware to control access to illegal routes, but there is a session variable that sends you to abort 502 and it has ALWAYS worked.
Today I have started to play the render for the handling of the exceptions produced by Mysql and now when I try to access a route without being logged in I miss the page of the Mysql error and it does not jump abort 502
Middlenware: It always worked:
Route::get('HomeJefe', [
'uses' => 'ControladorJefeDept@index',
'middleware' => ['ComprobarLogin', 'ComprobarJefeDept']]);
Check Login
public function handle($request, Closure $next)
{
if(\Session::has('conectado')){
return $next($request);
}else{
abort(502);
}
}
CheckJefeDept
public function handle($request, Closure $next) {
$jefe = False;
$conectado = \Session::get('conectado');
foreach ($conectado->getRol() as $tipoRol) {
if ($tipoRol == "Jefe Departamento")
$jefe = True;
}
if ($jefe) {
return $next($request);
} else {
abort(503);
}
}
Exceptions: handler:
public function render($request, Exception $e) {
$mensaje = $e->getMessage();
$mensaje = substr($mensaje, 0, 49);
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
\Session::put('mensaje', $mensaje);
return redirect('error');
// return parent::render($request, $e);
}
On the route:
Route::get('error', "ControladorSistema@error");
System controller:
class ControladorSistema extends Controller {
public function error(Request $request){
return view('errors/PagError');
}
}
Since I put that code in the render, it does not go to the abort (502) anymore but goes directly to the PagError view.
Solution ::
Exception handler with dd looks at the type of error it is and redirects:
public function render($request, Exception $e) {
//dd($e);
if ($e instanceof \Illuminate\Database\QueryException) {
$mensaje="Problemitas con la bbdd";
\Session::put('mensaje', $mensaje);
return redirect('error');
}
if ($e instanceof NotFoundHttpException) {
$mensaje="La pag no existe.";
\Session::put('mensaje', $mensaje);
return redirect('error');
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
return parent::render($request, $e);
}
//\Session::put('mensaje', $mensaje);
// return redirect('error');
//return parent::render($request, $e);
}