我正在开发一个Laravel全球中间件,以便在web访问者访问网站时在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”为空:
2-如果cookie geo不是空代码:($request->cookies->has('geo'))
(我对第2步的想法是针对已有国家/地区的现有客户(已有一个具有该值的cookie),但他们希望在static.select-country视图中手动更改国家/地区,并避免GeoIP探测器被循环1覆盖。)
我的问题:此时,当客户在static.select-country视图中手动选择时,它会移动到主页:
-
但是应用程序使用GeoIP检测器分配cookie,而不是考虑客户手动选择的国家(在static.select country中创建的cookie)。