Documentation/Container/Services/Cookies

Cookies

The HTTP cookies in the HLEB2 framework are handled by the Cookies service.

Examples of using Cookies in controllers (and all classes inheriting from Hleb\Base\Container), such as retrieving a value from cookies:

// variant 1
use Hleb\Reference\CookieInterface;
$value $this->container->get(CookieInterface::class)->get('cookie_name');

// variant 2
$value $this->container->cookies()->get('cookie_name');

// variant 3
$value $this->cookies()->get('cookie_name');

Example of accessing cookies in application code:

// variant 1
use Hleb\Static\Container;
use 
Hleb\Reference\CookieInterface;
$value Container::get(CookieInterface::class)->get('cookie_name');

// variant 2
use Hleb\Static\Cookies;
$value Cookies::get('cookie_name');

The Cookies object can also be obtained through dependency injection via the Hleb\Reference\Interface\Cookie interface.

To simplify examples, the following will only include access through Hleb\Static\Cookies.


#get()

The get() method returns the cookie by name as an object. Through this object, you can obtain both raw data and data transformed into the required format. The framework handles HTML tag transformation, which is necessary if the data is to be displayed on a page to avoid potential cookie-based XSS vulnerabilities.
The example shows various ways to retrieve the cookie value:

use Hleb\Static\Cookies;

// (!) Original raw data.
$rawValue Cookies::get('cookie_name')->value();

// Validated data converted to string.
$clearedStringValue Cookies::get('cookie_name')->asString();

// Data converted to an integer.
$integerValue Cookies::get('cookie_name')->asInt();

// Data checked for a positive integer.
$positiveIntegerValue Cookies::get('cookie_name')->asPositiveInt();

#all()

The all() method returns a named array of objects similar to those obtained with the get() method, from which you can retrieve values of all or specific cookies.

The most common error when using the object returned by these methods is treating the object as a value instead of retrieving the value from the object.


#set()

The set() method is used to set or update a cookie by its name. The first argument is the cookie name, the second one is the value to be assigned. The third argument 'options' expects an array of additional parameters, similar to the PHP function setcookie(), where you can set options like 'expires', 'path', 'domain', 'secure', 'httponly', and 'samesite'.

use Hleb\Static\Cookies;

$options = [
    
'expires' => time() + 60 60 24 30,
    
'path' => '/',
    
'domain' => '.example.com'// leading dot for compatibility or use subdomain
    
'secure' => true,     // or false
    
'httponly' => true,    // or false
    
'samesite' => 'None' // None / Lax / Strict
];
Cookies::set('cookie_name''value'$options);

#delete()

The delete() method is used for deleting a cookie by its name.


#clear()

The clear() method allows you to clear all cookies.


#Asynchronous Mode

In the asynchronous usage of the framework, the methods of the Cookies service function similarly, but a different mechanism is used for setting and reading them.

Sessions Redirect

Page translated: chatgpt 4-o
Back to top