mardi 17 avril 2018

Infinite loop in redirect to action inside a middleware

I have a middleware wrapping all routes to detect the user ip, the problem is that i make the redirect to controller action and i'm getting an infinite loop with a 302 error code.

Middleware

protected $auth;
protected $userIP;

public function __construct()
{
    $this->userIP = ( !empty($_SERVER["HTTP_CF_IPCOUNTRY"]) ) ? $_SERVER["HTTP_CF_IPCOUNTRY"] : false;
    $this->auth = Auth::user();

    if ($this->auth->guest() || $this->auth->user()->isAnonymous()) {
        $this->auth = null;
    }
}

public function handle($request, Closure $next)
{
    if( $this->userIP ):
    $country = ($this->userIP == "US") ? "USA" : (($this->userIP == "CA") ? "CANADA" : (($this->userIP == "CN") ? "CHINA" : (($this->userIP == "PA") ? "PANAMA" : (($this->userIP == "GT") ? "GUATEMALA" : "MX"))));

    switch($country)
    {
        case "USA":
            return redirect()->away('http://mysiteusa.com/');
            break;

        case "GUATEMALA":
            return redirect()->action('Site\SwitchCountryController@update', ['key' => 8]);
            break;
    }

    endif;

    return $next($request);
}

Controller

public function update($clave)
    {
        $newSucursal = Sucursal::firstOnCountry($key);

        if (! $newSucursal) {
            return back();
        }

        user()->setStore($newSucursal);

        return redirect('/products');
    }

I'm a little lost about how to fix this, any idea?

Regards!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire