Shorthands for cookie values #6

Closed
opened 2026-04-27 11:58:36 +02:00 by c.fahner · 1 comment
Owner

Currently the code to extract cookie values using the library abstractions looks like this:

$cookieHeader = Fields::getFirst($request->headers, 'cookie');
if ($cookieHeader !== null && $cookieHeader instanceof Structured) {
	$cookieValue = $cookies->root['cookieName'];
	if (\is_scalar($cookieValue)) {
    	// do something with it
	}
}

This is an unacceptable amount of code for a library that is supposed to make things like this easier, considering the "PHP way" to get this information is simply referencing $_COOKIES['cookieName'].

This issue introduces a utility class Slendium\Http\Message\Cookies with a method get(Message|iterable<Field> $source, string $name):?string. Passing the method a Message extracts the cookie value from its $headers.

Currently the code to extract cookie values using the library abstractions looks like this: ```php $cookieHeader = Fields::getFirst($request->headers, 'cookie'); if ($cookieHeader !== null && $cookieHeader instanceof Structured) { $cookieValue = $cookies->root['cookieName']; if (\is_scalar($cookieValue)) { // do something with it } } ``` This is an unacceptable amount of code for a library that is supposed to make things like this easier, considering the "PHP way" to get this information is simply referencing `$_COOKIES['cookieName']`. This issue introduces a utility class `Slendium\Http\Message\Cookies` with a method `get(Message|iterable<Field> $source, string $name):?string`. Passing the method a `Message` extracts the cookie value from its `$headers`.
c.fahner added this to the v0.2 milestone 2026-04-27 11:58:36 +02:00
Author
Owner

Note: also update the README example.

Note: also update the README example.
c.fahner referenced this issue from a commit 2026-04-29 16:50:18 +02:00
c.fahner stopped working 2026-04-29 16:51:17 +02:00
14 minutes 19 seconds
c.fahner added spent time 2026-04-29 17:40:28 +02:00
10 minutes
c.fahner added spent time 2026-05-05 06:53:35 +02:00
5 minutes
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Total time spent: 29 minutes 19 seconds
c.fahner
29 minutes 19 seconds
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
slendium/http#6
No description provided.