Laravel 设置国家/地区值的全局中间件逻辑失败
Laravel Global Middleware to set Country Value failed in its logic
我正在开发 Laravel 全局中间件,以便在网络访问者访问网站时在 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' 为空且 Torann GeoIp 检测器不为空 -> 创建一个包含国家 ISO_CODE
的 cookie
- 否则(地理 cookie 为空 && GeoIP 检测器为空)-> 转到页面 select-country.phtml 以选择您的国家并手动设置 cookie。
2- 如果 cookie geo 不是空代码:($request->cookies->has('geo'))
- 访客已有 cookie -> 移至页面。
(我对第 2 步的想法是针对已有国家(已有具有该值的 cookie)但他们想在 static.select-国家视图中手动更改国家并避免使用 GeoIP 检测器的现有客户由循环 1 覆盖。)
我的问题:此时当客户在static.select-国家视图中手动选择时,它移动到首页:
- 但应用程序使用 GeoIP 检测器分配一个 cookie,而不是考虑由客户手动 selected 国家(cookie 在 static.select-国家创建)。
您可以为那个 selecting 国家/地区页面创建一个路由,并在上面的中间件中忽略该路由。然后每个人都可以访问该页面并 select 从列表中拥有自己的国家并在 cookie 中设置该国家代码。
if ($request->is('YOUR_ROUTE_PATH')) {
return $next($request);
}
我正在开发 Laravel 全局中间件,以便在网络访问者访问网站时在 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' 为空且 Torann GeoIp 检测器不为空 -> 创建一个包含国家 ISO_CODE 的 cookie
- 否则(地理 cookie 为空 && GeoIP 检测器为空)-> 转到页面 select-country.phtml 以选择您的国家并手动设置 cookie。
2- 如果 cookie geo 不是空代码:($request->cookies->has('geo'))
- 访客已有 cookie -> 移至页面。
(我对第 2 步的想法是针对已有国家(已有具有该值的 cookie)但他们想在 static.select-国家视图中手动更改国家并避免使用 GeoIP 检测器的现有客户由循环 1 覆盖。)
我的问题:此时当客户在static.select-国家视图中手动选择时,它移动到首页:
- 但应用程序使用 GeoIP 检测器分配一个 cookie,而不是考虑由客户手动 selected 国家(cookie 在 static.select-国家创建)。
您可以为那个 selecting 国家/地区页面创建一个路由,并在上面的中间件中忽略该路由。然后每个人都可以访问该页面并 select 从列表中拥有自己的国家并在 cookie 中设置该国家代码。
if ($request->is('YOUR_ROUTE_PATH')) {
return $next($request);
}