PHPackages                             beebmx/kirby-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. beebmx/kirby-middleware

ActiveKirby-plugin[Security](/categories/security)

beebmx/kirby-middleware
=======================

Middleware provides a powerful mechanism for inspecting and filtering requests entering in your Kirby site.

1.3.0(10mo ago)82351MITPHPPHP ^8.2CI passing

Since Jun 28Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/beebmx/kirby-middleware)[ Packagist](https://packagist.org/packages/beebmx/kirby-middleware)[ RSS](/packages/beebmx-kirby-middleware/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (7)Versions (9)Used By (1)

[![Build Status](https://camo.githubusercontent.com/1a1267788a860baac9b75e2fdc219a7f7aa0f4a991f42b7b567f29080967c1f3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f626565626d782f6b697262792d6d6964646c65776172652f74657374732e796d6c3f6272616e63683d6d61696e)](https://github.com/beebmx/kirby-middleware/actions)[![Total Downloads](https://camo.githubusercontent.com/e2ebea3639ae7e880c75215e89a317c7967be3573ef4cfff52199dc8d778a8f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f626565626d782f6b697262792d6d6964646c6577617265)](https://packagist.org/packages/beebmx/kirby-middleware)[![Latest Stable Version](https://camo.githubusercontent.com/3f2e9e84d76906dc4b4454894eae875e6341ffbe598a20a0a0c17d7f3e57879b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f626565626d782f6b697262792d6d6964646c6577617265)](https://packagist.org/packages/beebmx/kirby-middleware)[![License](https://camo.githubusercontent.com/29a010ad7df8af191bd4b89493247aa334cd7c900b363b369967dac96c56ba1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f626565626d782f6b697262792d6d6964646c6577617265)](https://packagist.org/packages/beebmx/kirby-middleware)

Middleware for Kirby
====================

[](#middleware-for-kirby)

`Middleware` provides a powerful mechanism for inspecting and filtering requests entering your `Kirby` site.

---

Overview
--------

[](#overview)

- [1. Installation](#installation)
- [2. Usage](#usage)
- [3. Middleware](#middleware)
- [4. Options](#options)
- [5. Facades](#facades)
- [6. Plugins](#plugins)
- [7. Roadmap](#roadmap)
- [8. License](#license)
- [9. Credits](#credits)

Installation
------------

[](#installation)

### Download

[](#download)

Download and copy this repository to `/site/plugins/kirby-middleware`.

### Composer

[](#composer)

```
composer require beebmx/kirby-middleware

```

Usage
-----

[](#usage)

Out of the box, you don't need to do anything to start using (except for installation). When you install the `Middleware` package, it comes with two ways of management middlewares, `global` middlewares and `groups` of middlewares.

### Global middlewares

[](#global-middlewares)

This middleware will always be triggered in every `Page` by the `Middleware` handler. Out of the box comes with a `TrimStrings` middleware, which will remove spaces in the `Request` made.

Note

To access to this request, you should call the `Beebmx\KirbyMiddleware\Request` instance.

The `Kirby\Http\Request` instance will never be modified.

You can access to the `Request` instance transformed with:

```
use Beebmx\KirbyMiddleware\Request;

$request = Request::instance();
```

You can add features to the `global` middleware in your `config.php` file:

```
'beebmx.middleware' => [
    'global' => [
        MyOwnGlobalMiddleware::class,
    ],
],
```

Note

You can add as much middleware as requested.

They can be a `class` or a `Closure`.

#### TrimStrings

[](#trimstrings)

`TrimStrings` clean all the inputs in the request, but sometimes you need to ignore some `inputs` to be trimmed; you can skip it with:

```
'beebmx.middleware' => [
    'exceptions' => [
        'trim' => [
            'password',
            'password_confirmation',
        ],
    ],
],
```

And you can recover those `inputs` with the `Request` instance in your controllers, models or any place required with:

```
use Beebmx\KirbyMiddleware\Request;

Request::instance()->get('yourInput')
```

Or for your convinience you can use the facade:

```
use Beebmx\KirbyMiddleware\Facades\Request;

Request::get('yourInput')
```

### Group middlewares

[](#group-middlewares)

The group middlewares will depend on routes to be triggered. By default, the group middleware comes with the `web`, `auth` and `guest` middleware, it brings a `ValidateCsrfToken` middlewares.

You can set the routes by adding the `routes` values in your `config.php` file:

```
'beebmx.middleware' => [
    'routes' => [
        'web' => [
            'blog/(:any)',
            'content/(:alpha)',
            'page/(:num)',
        ]
    ],
],
```

Note

You can add a [pattern](https://getkirby.com/docs/reference/router/patterns) like any `Kirby` route

By default, the `web` group comes with the `(:all)` route.

The `auth` and `guest` middlewares are inactive by default, but you can customize the routes to enable them.

And of course, you can add more features to the `web` middleware in your `config.php` file:

```
'beebmx.middleware' => [
    'web' => [
        MyOwnMiddleware::class,
    ],
],
```

If the `web` group is not what you need, you can add a new group of middleware. You can add it within the `config.php` file:

```
'beebmx.middleware' => [
    'groups' => [
        MyOwnMiddlewareGroup::class,
    ],
],
```

The `Middleware Group` should looks like:

```
use Beebmx\KirbyMiddleware\MiddlewareGroups\MiddlewareGroup;

class MyOwnMiddlewareGroup extends MiddlewareGroup
{
    public string $name = 'review';

    public string|array|null $routes = [
        'blog/(:any)',
        'content/(:alpha)',
    ];

    public array $group = [
        ReviewBlogMiddleware::class,
        ReviewContentMiddleware::class,
        ReviewByAuthorMiddleware::class,
    ];
}
```

Important

All the group middleware classes should extend `Beebmx\KirbyMiddleware\MiddlewareGroups\MiddlewareGroup` class.

#### ValidateCsrfToken

[](#validatecsrftoken)

When you use an HTML form with `POST`, `PUT`, `PATCH`, or `DELETE` in your template, you should include a hidden CSRF `_token` field in the form so that the CSRF protection middleware can validate the request.

```
