3
I have some views that only clients can access, and some that only administrators can access. What I defined in this case is id_client, which if it is "1" is administrator and if it is any other client. I control it with 2 Middleware locally, but when I posted to a hosting server I get an error:
This web page has a redirect loop
ERR_TOO_MANY_REDIRECTS
Routes:
// Rotas para administradores
Route::group(['middleware' => 'auth', 'middleware' => 'SIST\Http\Middleware\AdminMiddleware'], function()
{
Route::get('inicio', ['as' => 'inicio','uses' => 'Admin\InicioController@index']);
});
// Rotas para clientes
Route::group(['middleware' => 'auth', 'middleware' => 'SIST\Http\Middleware\ClientMiddleware'], function ()
{
Route::get('sist', ['as' => 'sist','uses' => 'Client\SistController@index']);
});
Admin Middleware:
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (Auth::user()->id_cliente !== 1)
{
return redirect('/sist');
}else{
return $next($request);
}
}
}
Client Middleware:
class ClientMiddleware
{
public function handle($request, Closure $next)
{
$id_cliente = Auth::user()->id_cliente;
if ($id_cliente === 1)
{
return redirect('/inicio');
}else{
$cliente = Cliente::find($id_cliente);
return $next($request);
}
}
}
htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
I have many routes within these two groups and I use this to not let a client access an administrator route by typing the URL. I don’t know if this is the best way, but this is what I was able to do. If you have another way, please tell me.
See through your browser’s development tool which loop is occurring. Which two addresses are receiving redirects from each other. The problem may have nothing to do with your middleware, but with your DNS zone.
– Marco Aurélio Deleu
when logging in with a user that should lead to the "/start" directory, it is endlessly redirecting to the "/sist". The same happens if I try to log in with user who really should be directed to the "/sist", but the redirect never to.
– Raylan Soares
@Marcoauréliodeleu, it’s like he doesn’t recognize the conditions, I don’t know... I really have no idea. I edited the question by putting my htaccess.
– Raylan Soares