无法设置cookie

时间:2016-02-11 00:21:28

标签: php cookies laravel-5

即使没有返回错误,以下逻辑也不会设置任何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;
        }
    }
}

0 个答案:

没有答案