Laravel Global Middleware设置Country Value的逻辑失败



我正在开发Laravel Global中间件,以便在网络访问者访问网站时在Cookie中设置国家价值。

为此,我创建了以下函数:

public function handle($request, Closure $next)
{
if(!$request->cookies->has('geo'))
{
if (!$request->cookies->has('geo') && GeoIP()->getLocation()->iso_code !== null) {
//find customer IP location
$code = strtolower(GeoIP()->getLocation()->iso_code);
// creates a cookie with iso_code value
$cookie = cookie('geo', $code, 600);
//move to page
return $next($request)->cookie($cookie);
}
else{
return response()->view('static.select-country');
//move to page
return $next($request);
}
}
if ($request->cookies->has('geo')) {
//move to page
return $next($request);
}
}

1-如果cookie"geo"为空:

  • 首先:cookie"geo"为null,Torann GeoIp检测器不为null->创建国家ISO_CODE的cookie
  • 否则(地理cookie为空&&GeoIP检测器为空(->转到select-country.phtml页面以选择您的国家/地区并手动设置cookie

2-如果cookie地理位置不是空代码:($request->cookie->具有('geo'((

  • 访问者已经有一个cookie->移动到页面

(我对第2步的想法是针对已经有国家/地区(已经有一个具有该值的cookie(但他们希望在静态中手动更改国家/地区的现有客户。选择国家/地区视图并避免循环1覆盖GeoIP检测器。(

我的问题是:此时,当客户在静态选择国家/地区视图中手动选择时,它会移动到主页:

  • 但是应用程序使用GeoIP检测器分配cookie,而不是考虑客户手动选择的国家/地区(在static.select国家/地区创建的cookie(

您可以为选择的国家/地区页面创建一个路由,并在上面的中间件中忽略该路由。然后每个人都可以访问该页面,从列表中选择自己的国家,并在cookie中设置该国家代码。

if ($request->is('YOUR_ROUTE_PATH')) {
return $next($request);
}

相关内容

最新更新