An open API service indexing awesome lists of open source software.

https://github.com/dotkernel/dot-response-header

Configuration of all Response Headers
https://github.com/dotkernel/dot-response-header

Last synced: 7 months ago
JSON representation

Configuration of all Response Headers

Awesome Lists containing this project

README

          

# dot-response-header

`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/.

## Badges

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-response-header)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-response-header/3.5.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/network)
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/stargazers)
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/blob/3.0/LICENSE)

[![Build Static](https://github.com/dotkernel/dot-response-header/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-response-header/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-response-header/graph/badge.svg?token=NNRZN0FBF2)](https://codecov.io/gh/dotkernel/dot-response-header)
[![PHPStan](https://github.com/dotkernel/dot-response-header/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-response-header/actions/workflows/static-analysis.yml)

## Requirements

- **PHP**: 8.1, 8.2, 8.3 or 8.4

## Installation

Run the following command in your project root directory:

```shell
composer require dotkernel/dot-response-header
```

Next, register the package's `ConfigProvider` to your application config.

```php
Dot\ResponseHeader\ConfigProvider::class,
```

> 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);`

```php
$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
[
'*' => [
'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:

```php
'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`.