John Davidson

php - Setting cookies in Laravel Octane / Swoole

0 comments
Message:


I'm attempting to set cookies on Laravel Octane, however the cookies appear to be not defined on the given index.


setcookie(self::JWT_TOKEN_COOKIE, $token_details['access_token'], [
'httponly' => true,
'expires' => time() + $token_details['expires_in'],
'path' => '/'
]);

die($_COOKIE[self::JWT_TOKEN_COOKIE]);

When attempting to print the Cookie, I get an error telling me it hasnt been set.



Undefined array key "JWT_TOKEN"



Is there something wrong in the way I'm setting cookies in Laravel Octane? Is there a new class I should be using to use these as the application is now fully held in RAM/loaded once?



My Answer:
> To set cookies in Laravel Octane/Swoole, you can use the `response()->cookie()` method provided by Laravel. Here's an example of how you can set a cookie in your Octane/Swoole application:

php
use Illuminate\Support\Facades\Cookie;

$response = response('Hello World')->cookie(
'JWT_TOKEN_COOKIE',
$token_details['access_token'],
60, // expiration time in minutes
null,
null,
false,
true // httponly
);

return $response;


In this example, we are setting a cookie named `JWT_TOKEN_COOKIE` with the value of `$token_details['access_token']` and setting the `httponly` flag to `true`. You can adjust the expiration time and other parameters as needed.

Make sure to import the `Cookie` facade at the top of your file:

php
use Illuminate\Support\Facades\Cookie;


This should allow you to set cookies in your Laravel Octane/Swoole application.

Rate this post

4 of 5 based on 5295 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us