dotkernel / dot-response-header
Dotkernel middleware for setting custom response headers.
Requires
- php: ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
- mezzio/mezzio-router: ^3.16 || ^4.0
- psr/container: ^1.1 || ^2.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0
- laminas/laminas-diactoros: ^3.10
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5.22
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
dot-response-header is Dotkernel's middleware for setting and overwriting custom response headers.
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-response-header/.
Versions
| Version | Supported | PHP |
|---|---|---|
| 4.x | ✅ | 8.3 to 8.6 |
| 3.x | Security fixes only | 8.2 to 8.5 |
| <= 2.x | ❌ |
Upgrading from 3.x? See Upgrading to 4.x.
Badges
Requirements
- PHP: 8.3, 8.4, 8.5 or 8.6
Installation
Run the following command in your project root directory:
composer require dotkernel/dot-response-header
Next, register the package's ConfigProvider to your application config.
Dot\ResponseHeader\ConfigProvider::class,
Make sure to register the package under the
// DK packagessection.
After registering the package, add it to the middleware stack in config/pipeline.php after $app->pipe(RouteMiddleware::class);
$app->pipe(RouteMiddleware::class); $app->pipe(\Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware::class);
Create a new file response-header.global.php in config/autoload with the below configuration array:
<?php return [ 'dot_response_headers' => [ '*' => [ 'CustomHeader1' => [ 'value' => 'CustomHeader1-Value', 'overwrite' => true, ], 'CustomHeader2' => [ 'value' => 'CustomHeader2-Value', 'overwrite' => false, ], ], 'home' => [ 'CustomHeader' => [ 'value' => 'header3', ], ], 'login' => [ 'LoginHeader' => [ 'value' => 'LoginHeader-Value', 'overwrite' => false, ], ], ], ];
Because headers are matched with route names, we can have custom response headers for every request, by defining new headers under the * key.
All headers under * will be set for every response.
To add response headers for a specific route, define a new array using the exact route name as the array key.
Example:
'dot_response_headers' => [ 'user' => [ 'UserCustomHeader' => [ 'value' => 'UserCustomHeader-Value', 'overwrite' => false, ], ], ], // This will set a new header named UserCustomHeader with the UserCustomHeader-Value value for the route named 'user'
To overwrite an existing header use overwrite => true. The * headers are applied before the route headers, so a route header replaces a * header with the same name only when it sets overwrite => true.
An invalid configuration throws Dot\ResponseHeader\Exception\InvalidConfigException when the middleware is created, with a message naming the key.