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' 为空:

2- 如果 cookie geo 不是空代码:($request->cookies->has('geo'))

(我对第 2 步的想法是针对已有国家(已有具有该值的 cookie)但他们想在 static.select-国家视图中手动更改国家并避免使用 GeoIP 检测器的现有客户由循环 1 覆盖。)

我的问题:此时当客户在static.select-国家视图中手动选择时,它移动到首页:

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

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