我有一个中间件包装所有路由以检测用户ip,问题是我重定向到控制器操作,并且我得到一个带有302错误代码的无限循环。
中间件
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);
}
控制器
public function update($clave)
{
$newSucursal = Sucursal::firstOnCountry($key);
if (! $newSucursal) {
return back();
}
user()->setStore($newSucursal);
return redirect('/products');
}
我对如何解决这个问题有点失落,任何想法?
问候!