即使没有返回错误,以下逻辑也不会设置任何cookie。
<?php
namespace App\Libraries;
use Cookie;
use Location;
use Illuminate\Http\Request;
class GeoLocation
{
public $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function getLocationByIP()
{
$location = Location::get();
// Get the location by city
return $location->cityName;
}
public function getLocation()
{
// Check if the location is already set in the cookie
if ($this->request->cookie('location')) {
return $this->request->cookie('location');
} else {
$location = $this->getLocationByIP();
// Set location in the cookie
Cookie::queue('location', $location, 1440);
return $location;
}
}
}