laminas-api-tools / api-tools-http-cache
Laminas Module providing http cache headers support
Fund package maintenance!
Community Bridge
Installs: 73 185
Dependents: 1
Suggesters: 3
Security: 0
Stars: 5
Watchers: 10
Forks: 6
Open Issues: 2
Requires
- php: ^7.3 || ~8.0.0 || ~8.1.0
- laminas/laminas-eventmanager: ^3.0.1
- laminas/laminas-http: ^2.13
- laminas/laminas-mvc: ^3.1
- laminas/laminas-zendframework-bridge: ^1.1
Requires (Dev)
- container-interop/container-interop: ^1.1
- laminas/laminas-coding-standard: ~2.3.0
- phpunit/phpunit: ^9.3
- psalm/plugin-phpunit: ^0.16.0
- vimeo/psalm: ^4.2
Replaces
- zfcampus/zf-http-cache: ^1.4.0
README
π·πΊ Π ΡΡΡΠΊΠΈΠΌ Π³ΡΠ°ΠΆΠ΄Π°Π½Π°ΠΌ
ΠΡ, ΡΡΠ°ΡΡΠ½ΠΈΠΊΠΈ Laminas, ΡΠΎΠ΄ΠΈΠ»ΠΈΡΡ ΠΈ ΠΆΠΈΠ²Π΅ΠΌ Π² ΡΠ°Π·Π½ΡΡ ΡΡΡΠ°Π½Π°Ρ . Π£ ΠΌΠ½ΠΎΠ³ΠΈΡ ΠΈΠ· Π½Π°Ρ Π΅ΡΡΡ Π΄ΡΡΠ·ΡΡ, ΡΠΎΠ΄ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΈ ΠΈ ΠΊΠΎΠ»Π»Π΅Π³ΠΈ ΠΊΠ°ΠΊ Π² Π ΠΎΡΡΠΈΠΈ, ΡΠ°ΠΊ ΠΈ Π² Π£ΠΊΡΠ°ΠΈΠ½Π΅. ΠΠ΅ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠ· Π½Π°Ρ ΡΠΎΠ΄ΠΈΠ»ΠΈΡΡ Π² Π ΠΎΡΡΠΈΠΈ. ΠΠ΅ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠ· Π½Π°Ρ ΠΆΠΈΠ²ΡΡ Π² Π ΠΎΡΡΠΈΠΈ. Π£ Π½Π΅ΠΊΠΎΡΠΎΡΡΡ Π±Π°Π±ΡΡΠΊΠΈ ΠΈ Π΄Π΅Π΄ΡΡΠΊΠΈ ΡΡΠ°ΠΆΠ°Π»ΠΈΡΡ Ρ ΡΠ°ΡΠΈΡΡΠ°ΠΌΠΈ Π²ΠΎ ΠΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΎΠ²ΠΎΠΉ Π²ΠΎΠΉΠ½Π΅. ΠΠ΄Π΅ΡΡ Π½ΠΈΠΊΡΠΎ Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΡΠ°ΡΠΈΠ·ΠΌ.
Π£ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· Π½Π°Ρ Π΅ΡΡΡ ΡΠΊΡΠ°ΠΈΠ½ΡΠΊΠ°Ρ ΡΠΎΠ΄ΡΡΠ²Π΅Π½Π½ΠΈΡΠ°, ΠΊΠΎΡΠΎΡΠ°Ρ ΡΠΏΠ°ΡΠ»Π°ΡΡ ΠΈΠ· Π΄ΠΎΠΌΠ° Π²ΠΌΠ΅ΡΡΠ΅ Ρ ΡΡΠ½ΠΎΠΌ. ΠΠΎΠ΅Π·Π΄ Π·Π°Π΄Π΅ΡΠΆΠ°Π»ΡΡ ΠΈΠ·-Π·Π° Π±ΠΎΠΌΠ±Π΅ΠΆΠΊΠΈ Π½Π° Π΄ΠΎΡΠΎΠ³Π΅ Π²ΠΏΠ΅ΡΠ΅Π΄ΠΈ. Π£ Π½Π°Ρ Π΅ΡΡΡ Π΄ΡΡΠ·ΡΡ, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΡΡΡΡΡΡ Π² Π±ΠΎΠΌΠ±ΠΎΡΠ±Π΅ΠΆΠΈΡΠ°Ρ . ΠΡ Ρ ΡΡΠ΅Π²ΠΎΠ³ΠΎΠΉ ΠΆΠ΄Π΅ΠΌ Π²Π΅ΡΡΠΎΡΠΊΠΈ ΠΎΡ Π½ΠΈΡ ΠΏΠΎΡΠ»Π΅ Π²ΠΎΠ·Π΄ΡΡΠ½ΡΡ Π½Π°Π»Π΅ΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ Π±Π΅ΡΠΏΠΎΡΡΠ΄ΠΎΡΠ½ΠΎ Π½Π°Π½ΠΎΡΡΡ ΡΠ΄Π°ΡΡ ΠΈ ΠΏΠΎΠΏΠ°Π΄Π°ΡΡ ΠΏΠΎ Π±ΠΎΠ»ΡΠ½ΠΈΡΠ°ΠΌ, ΡΠΊΠΎΠ»Π°ΠΌ, Π΄Π΅ΡΡΠΊΠΈΠΌ ΡΠ°Π΄Π°ΠΌ ΠΈ Π΄ΠΎΠΌΠ°ΠΌ. ΠΡ Π½Π΅ Π±Π΅ΡΠ΅ΠΌ ΡΡΠΎ ΠΈΠ· ΠΊΠ°ΠΊΠΈΡ -Π»ΠΈΠ±ΠΎ Π‘ΠΠ. ΠΡ Π½Π°Π±Π»ΡΠ΄Π°Π΅ΠΌ ΡΡΠΎ Π½Π°ΠΏΡΡΠΌΡΡ.
ΠΡ Π΄ΠΎΠ²Π΅ΡΡΠ΅ΡΠ΅ Π½Π°ΠΌ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ, ΡΡΠΎΠ± ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π½Π°ΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, ΠΈ ΠΌΡ ΠΏΡΠΎΡΠΈΠΌ Π²Π°Ρ Π΄ΠΎΠ²Π΅ΡΠΈΡΡΡΡ Π½Π°ΠΌ Π²Π½ΠΎΠ²Ρ. ΠΡ Π½ΡΠΆΠ΄Π°Π΅ΠΌΡΡ Π² ΠΏΠΎΠΌΠΎΡΠΈ. ΠΡΡ ΠΎΠ΄ΠΈΡΠ΅ ΠΈ ΠΏΡΠΎΡΠ΅ΡΡΡΠΉΡΠ΅ ΠΏΡΠΎΡΠΈΠ² ΡΡΠΎΠΉ Π±Π΅ΡΠΏΠΎΠ»Π΅Π·Π½ΠΎΠΉ Π²ΠΎΠΉΠ½Ρ. ΠΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ ΠΊΡΠΎΠ²ΠΎΠΏΡΠΎΠ»ΠΈΡΠΈΠ΅. Π‘ΠΊΠ°ΠΆΠΈΡΠ΅ "ΠΠ΅Ρ Π²ΠΎΠΉΠ½Π΅!"
πΊπΈ To Citizens of Russia
We at Laminas come from all over the world. Many of us have friends, family and colleagues in both Russia and Ukraine. Some of us were born in Russia. Some of us currently live in Russia. Some have grandparents who fought Nazis in World War II. Nobody here supports fascism.
One team member has a Ukrainian relative who fled her home with her son. The train was delayed due to bombing on the road ahead. We have friends who are hiding in bomb shelters. We anxiously follow up on them after the air raids, which indiscriminately fire at hospitals, schools, kindergartens and houses. We're not taking this from any media. These are our actual experiences.
You trust us enough to use our software. We ask that you trust us to say the truth on this. We need your help. Go out and protest this unnecessary war. Stop the bloodshed. Say "stop the war!"
Introduction
api-tools-http-cache
is a Laminas module for automating http-cache tasks within a Laminas
application.
Installation
Run the following composer
command:
$ composer require "laminas-api-tools/api-tools-http-cache:^1.0"
Alternately, manually add the following to your composer.json
, in the require
section:
"require": { "laminas-api-tools/api-tools-http-cache": "^1.0" }
And then run composer update
to ensure the module is installed.
Finally, add the module name to your project's config/application.config.php
under the modules
key:
return [ /* ... */ 'modules' => [ /* ... */ 'Laminas\ApiTools\HttpCache', ], /* ... */ ];
Configuration
User Configuration
As a rule of thumb, avoid as much as possible using anonymous functions since it prevents you from caching your configuration.
The top-level configuration key for user configuration of this module is api-tools-http-cache
.
The config/module.config.php
file contains a self-explanative example of configuration.
Key: controllers
The controllers
key is utilized for mapping any of
- a route name
- a concatenated
controller::action
- a controller
- a regexp
- a wildcard
Each is case sensitive, and will map one or more HTTP methods to the cache header configuration specific to the given rule.
Example:
// See `config/module.config.php` for a complete commented example 'api-tools-http-cache' => [ /* ... */ 'controllers' => [ '<controller>' => [ '<http-method>' => [ '<cache-header-name>' => [ 'override' => true, 'value' => '<cache-header-value>', ], ], ], ], /* ... */ ],
Key: <controller>
Either
- a concatenation of
$controller::$action
- a controller name (as returned by
Laminas\Mvc\MvcEvent::getRouteMatch()->getParam('controller')
; the value is case-sensitive) - a regexp (see
<regex_delimiter>
key) - a wildcard
A wildcard matches any unspecified controllers.
Key: <http-method>
Either a lower cased HTTP method (get
, post
, etc.) (as returned by Laminas\Http\Request::getMethod()
) or a wildcard.
A wildcard stands for all the non-specified HTTP methods.
Key: <cache-header-name>
An HTTP cache header name (Cache-control
, expires
, etag
etc.).
ETags
For ETags you can specify a custom generator in the configuration:
'etag' => [
'override' => true,
'generator' => 'Your\Own\ETagGenerator',
],
A generator example can be found in \Laminas\ApiTools\HttpCache\DefaultETagGenerator
.
Key: <cache-header-value>
The value for the cache header.
Key: override
Whether to override the cache headers possibly sent by your application.
Key: enable
The enable
key is utilized for enabling/disabling the http cache module at run time.
If you no longer need this module, rather consider removing the module from the application.config.php
list.
Caution: when disabled, http cache module doesn't override/remove the cache headers sent by your application.
Example:
'api-tools-http-cache' => [ /* ... */ 'enable' => true, // Cache module is enabled. /* ... */ ],
Key: http_codes_black_list
The http_codes_black_list
is utilized to avoid caching the responses with the listed HTTP status codes.
Defaults to all others than 200
.
Example:
'api-tools-http-cache' => [ /* ... */ 'http_codes_black_list' => ['201', '304', '400', '500'], // Whatever the other configurations, the responses with these HTTP codes won't be cached. /* ... */ ],
Key: regex_delimiter
This key is used to enable the evaluation of the key as a regular expression.
It must contain the delimiter of the regular expression.
If you don't want to use regular expression in your configuration set this to null to avoid inutil parsing.
Regular expressions are tested in the very order they appear in the configuration, first matching wins.
Regexp wins over wildcard.
Caution: When this value is not empty and no litteral key corresponds to the current controller, a preg_match is used.
Example:
'api-tools-http-cache' => [ /* ... */ 'regex_delimiter' => '~', /* ... */ 'controllers' => [ '~.*~' => [ // Acts as a wildcard. /* ... */ ], ], /* ... */ ],
System Configuration
The following configuration is provided in config/module.config.php
:
'service_manager' => [ 'factories' => [ 'Laminas\ApiTools\HttpCache\HttpCacheListener' => 'Laminas\ApiTools\HttpCache\HttpCacheListenerFactory', ], ],
Laminas Events
Listeners
Laminas\ApiTools\HttpCache\HttpCacheListener
This listener is attached to the MvcEvent::EVENT_ROUTE
and MvcEvent::EVENT_FINISH
events with the low priority of -10000
.