dotkernel / dot-response-header
DotKernel middleware for setting custom response headers.
Installs: 11 574
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 1
Open Issues: 1
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- mezzio/mezzio-router: ^3.16
- psr/http-client: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0
- laminas/laminas-diactoros: ^3.1
- phpunit/phpunit: ^10.2
- vimeo/psalm: ^5.13
README
Middleware for setting and overwriting custom response headers.
Requirements
- PHP >= 8.1
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,
Note: Make sure to register the package under the // DK packages
section.
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 set of routes, define a new array using the 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 any route name matching 'user'
To overwrite an existing header use overwrite => true
.