dot-response-header is Dotkernel's middleware for setting and overwriting custom response headers.
Documentation is available at: https://docs.dotkernel.org/dot-response-header/.
| 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.
- PHP: 8.3, 8.4, 8.5 or 8.6
Run the following command in your project root directory:
composer require dotkernel/dot-response-headerNext, 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.