I have some views that only clients can access, and some that only administrators can access. What I defined in the case is the client_id, which if "1" is an administrator and if it is any other client. I control this with 2 Middleware locally, but when I published 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 the admin route by typing the URL. I do not know if that's the best way, but that's what I got. If you have any other way, please let me know.