1
1
# Polymorphine/Headers
2
- [ ![ Latest Stable Version ] ( https://poser.pugx.org/polymorphine/headers/version )] ( https://packagist.org/packages/polymorphine/headers )
2
+ [ ![ Latest stable release ] ( https://poser.pugx.org/polymorphine/headers/version )] ( https://packagist.org/packages/polymorphine/headers )
3
3
[ ![ Build status] ( https://github.com/polymorphine/headers/workflows/build/badge.svg )] ( https://github.com/polymorphine/headers/actions )
4
4
[ ![ Coverage status] ( https://coveralls.io/repos/github/polymorphine/headers/badge.svg?branch=develop )] ( https://coveralls.io/github/polymorphine/headers?branch=develop )
5
5
[ ![ PHP version] ( https://img.shields.io/packagist/php-v/polymorphine/headers.svg )] ( https://packagist.org/packages/polymorphine/headers )
6
6
[ ![ LICENSE] ( https://img.shields.io/github/license/polymorphine/headers.svg?color=blue )] ( LICENSE )
7
7
### HTTP Response headers middleware
8
8
9
9
### Installation with [ Composer] ( https://getcomposer.org/ )
10
- php composer.phar require polymorphine/headers
10
+ ``` bash
11
+ composer require polymorphine/headers
12
+ ```
11
13
12
14
### Basic usage
13
- ##### * Set-Cookie header*
14
15
16
+ ##### * Set-Cookie header*
15
17
1 . Instantiate a cookie builder using ` ResponseHeaders ` context:
16
-
17
- $headers = new ResponseHeaders();
18
- $cookieSetup = new CookieSetup($headers);
19
-
18
+ ``` php
19
+ $headers = new ResponseHeaders();
20
+ $cookieSetup = new CookieSetup($headers);
21
+ ```
20
22
Alternatively, instantiating ` CookieSetup ` is possible with ` ResponseHeaders ` method:
21
-
22
- $cookieSetup = $context->cookieSetup();
23
-
23
+ ``` php
24
+ $cookieSetup = $context->cookieSetup();
25
+ ```
24
26
2 . Configure cookie with array of its directives/attributes
25
- (see [ ` CookieSetup::directives() ` ] ( /src/Cookie/CookieSetup.php#L48 ) method):
26
-
27
- $cookieSetup->directives([
28
- 'Domain' => 'example.com',
29
- 'Path' => '/admin',
30
- 'Expires' => new DateTime(...),
31
- 'MaxAge' => 1234,
32
- 'Secure' => true,
33
- 'HttpOnly' => true,
34
- 'SameSite' => 'Strict'
35
- ]);
36
-
27
+ (see [ ` CookieSetup::directives() ` ] ( /src/Cookie/CookieSetup.php#L51 ) method):
28
+ ``` php
29
+ $cookieSetup->directives([
30
+ 'Domain' => 'example.com',
31
+ 'Path' => '/admin',
32
+ 'Expires' => new DateTime(...),
33
+ 'MaxAge' => 1234,
34
+ 'Secure' => true,
35
+ 'HttpOnly' => true,
36
+ 'SameSite' => 'Strict'
37
+ ]);
38
+ ```
37
39
Modifying setup object is also possible with its builder methods:
38
-
39
- $cookieSetup->domain('example.com')
40
- ->path('/admin')
41
- ->expires(new DateTime(...))
42
- ->maxAge(1234)
43
- ->secure()
44
- ->httpOnly()
45
- ->sameSite('Strict');
46
-
40
+ ``` php
41
+ $cookieSetup->domain('example.com')
42
+ ->path('/admin')
43
+ ->expires(new DateTime(...))
44
+ ->maxAge(1234)
45
+ ->secure()
46
+ ->httpOnly()
47
+ ->sameSite('Strict');
48
+ ```
47
49
3 . Instantiate [ ` Cookie ` ] ( /src/Cookie.php ) type object with its name:
48
-
49
- $cookie = $cookieSetup->cookie('MyCookie');
50
-
50
+ ``` php
51
+ $cookie = $cookieSetup->cookie('MyCookie');
52
+ ```
51
53
4 . Send value:
52
-
53
- $cookie->send('value');
54
-
54
+ ``` php
55
+ $cookie->send('value');
56
+ ```
55
57
or order to revoke cookie, so that it should not be sent with future requests:
56
-
57
- $cookie->revoke();
58
-
58
+ ``` php
59
+ $cookie->revoke();
60
+ ```
59
61
Each cookie can send/revoke header only once
60
62
61
63
##### Directives and Attributes
@@ -67,15 +69,15 @@ Concise description with additional class logic is explained in docBlocks of mut
67
69
of [ ` CookieSetup ` ] ( src/Cookie/CookieSetup.php ) class.
68
70
69
71
Here are some class-specific rules for setting those directives:
70
- * Empty values and root path (` / ` ) might be omitted as they're same as default.
71
- * ` SameSite ` allowed values are ` Strict ` or ` Lax ` , but ` Lax ` will be set for any non-empty value given.
72
- * ` Expires ` and ` MaxAge ` are different ways to set the same cookie's expiry date.
72
+ - Empty values and root path (` / ` ) might be omitted as they're same as default.
73
+ - ` SameSite ` allowed values are ` Strict ` or ` Lax ` , but ` Lax ` will be set for any non-empty value given.
74
+ - ` Expires ` and ` MaxAge ` are different ways to set the same cookie's expiry date.
73
75
If both directives will be passed into constructor or ` directivesArray() ` method,
74
76
last value will be used due to overwrite.
75
77
76
78
##### Cookie with predefined directives
77
79
78
80
` CookieSetup ` has two alternative methods creating ` Cookie ` instance: ` CookieSetup::permanentCookie() ` and
79
81
` CookieSetup::sessionCookie() ` .
80
- * * Permanent* constructor sets long (5 years) expiry values (` Expires ` and ` MaxAge ` )
81
- * * Session* constructor sets security directives (` HttpOnly ` and ` SameSite=Lax ` )
82
+ - * Permanent* constructor sets long (5 years) expiry values (` Expires ` and ` MaxAge ` )
83
+ - * Session* constructor sets security directives (` HttpOnly ` and ` SameSite=Lax ` )
0 commit comments