所以在__construct()中使用Cookies并没有看到由于我认为的会话以及laravel开发人员说它对于debuging不好的事情而正常工作,但对于我的情况,在构造上使用它会完全没问题,因为它的只是为了让类变量,有一种方法可以修复它或更好的方法来解决这个问题?我正在使用存储库让我的控制器控制guzzle客户端。
class ConnectionRepository implements ConnectionRepositoryInterface
{
public function __construct(Client $client)
{
$this->client = $client;
$this->setClientVariables();
$this->setOptions();
//dd($this->options);
//Gives null when Cookie is not set, gives wrong value if Cookie is set
}
public function get($url)
{
$this->setClientVariables();
$this->setOptions();
//dd($this->options);
//Gives null when Cookie is not set, gives right value if Cookie is set
$response = $this->client->request('GET', $url, $this->options);
return json_decode($response->getBody(), true);
}
public function setClientVariables()
{
$this->accessToken = Cookie::get('access_token');
$this->refreshToken = Cookie::get('refresh_token');
$this->user = Cookie::get('user');
}
public function setOptions()
{
$this->options = [
'base_uri' => $this->baseUri."/",
'http_errors' => false,
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->accessToken}",
]
];
}
}