vendredi 14 décembre 2018

Laravel Global Middleware to set Country Value failed in its logic

Im working on a Laravel Global Middleware to set Country Value in a Cookie when web visitors access the website.

For that reason I create the following function:

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- If cookie 'geo' is null:

  • First: cookie 'geo' is null and Torann GeoIp detector is not null -> create a cookie with country ISO_CODE
  • Else (geo cookie is null && GeoIP detector is null) -> go to page select-country.phtml in order to choose your country and set the cookie manually.

2- If cookie geo is not null code: ($request->cookies->has('geo'))

  • Visitor already has a cookie -> move to page.

(my idea for step 2 is for customers that want to change manually the country in static.select-country view and avoid GeoIP detector override by loop 1.)

My problem: at this moment when customer choose manually in static.select-country view, it moves to page home:

  • But the application assign a cookie with GeoIP detector instead of take into consideration manually selected country (cookie created in static.select-country) by customer.


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire