PHPackages                             benycode/slim-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. benycode/slim-middleware

ActiveLibrary

benycode/slim-middleware
========================

Slim 4 middleware package

v1.5.0(2y ago)011PHPPHP ^8.0

Since Jan 18Pushed 2y ago1 watchersCompare

[ Source](https://github.com/benycode/slim-middleware)[ Packagist](https://packagist.org/packages/benycode/slim-middleware)[ RSS](/packages/benycode-slim-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)DependenciesVersions (8)Used By (0)

Slim Middleware bundle
======================

[](#slim-middleware-bundle)

A Slim 4 Framework useful middlewares.

Features
--------

[](#features)

- health check endpoint;
- info endpoint;
- settings setup;
- exception handler;
- APISIX auto route register;
- Leader election middleware;
- Endpoint protection with X-Api-Token.

Table of contents
-----------------

[](#table-of-contents)

- [Install](#install)
- \[Health check endpoint usage\](#health check endpoint usage)
- \[Info endpoint usage\](#info endpoint usage)
- \[Settings setup usage\](#settings setup usage)
- \[Exception handler usage\](#exception handler usage)
- \[APISIX auto route register usage\](#apisix auto route register usage)
- \[Leader election usage\](#leader election usage)
- \[Endpoint protection with X-Api-Token usage\](#endpoint protection with X-Api-Token usage)

Install
-------

[](#install)

Via Composer

```
$ composer require benycode/slim-middleware
```

Requires Slim 4.

Health check endpoint usage
---------------------------

[](#health-check-endpoint-usage)

Use [DI](https://www.slimframework.com/docs/v4/concepts/di.html) to inject the library Middleware classes:

```
use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware;

return [
    ......
    HealthCheckEndpointMiddleware::class => function (ContainerInterface $container) {
        return new HealthCheckEndpointMiddleware(
           [
              'health_endpoint' => '/_health', // change if needed other endpoint
           ],
           ,
        );
    },
    ......
];
```

add the **Middleware** to `any` route at the end of the routes:

```
use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(HealthCheckEndpointMiddleware::class)
   ->setName('any')
   ;
```

welcome, your app is within new path:

- /\_health or your defined

create health check.

Info endpoint usage
-------------------

[](#info-endpoint-usage)

Use [DI](https://www.slimframework.com/docs/v4/concepts/di.html) to inject the library Middleware classes:

```
use BenyCode\Slim\Middleware\InfoEndpointMiddleware;

return [
    ......
    InfoEndpointMiddleware::class => function (ContainerInterface $container) {
        return new InfoEndpointMiddleware(
           [
              'info_endpoint' => '/_info', // change if needed other endpoint
           ],
           '', // example: v0.0.0
        );
    },
    ......
];
```

add the **Middleware** to `any` route at the end of the routes:

```
use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\InfoEndpointMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(InfoEndpointMiddleware::class)
   ->setName('any')
   ;
```

welcome, your app is within new path:

- /\_info

Settings setup usage
--------------------

[](#settings-setup-usage)

add the **Middleware** to a `global` list:

```
use BenyCode\Middleware\SettingsUpMiddleware;

return function (App $app) {
        ...
        $app->add(SettingsUpMiddleware::class);
        ...
};
```

get the settings:

```
protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
   $settings = $request
      ->getAttribute('settings')
   ;
}
```

Exception handler usage
-----------------------

[](#exception-handler-usage)

add the **Middleware** to a `global` list:

```
use BenyCode\Middleware\ExceptionMiddleware;

return function (App $app) {
        ...
        $app->add(ExceptionMiddleware::class);
        ...
};
```

welcome, your app is within new error handler.

APISIX auto route register usage
--------------------------------

[](#apisix-auto-route-register-usage)

Idea: Auto create service and routes on the health check procedure.

You can use it with:

- Docker health check;
- k8s health check;
- and more others....

Requires `curl` and `docker/k8s` health check mechanism.

Balanced with `LeaderElectionMiddleware`, bring more stability and activate one instance registration functionality.

Use [DI](https://www.slimframework.com/docs/v4/concepts/di.html) to inject the library Middleware classes:

```
use BenyCode\Slim\Middleware\APISIXRegisterMiddleware;

return [
    ......
    APISIXRegisterMiddleware::class => function (ContainerInterface $container) {
       return new APISIXRegisterMiddleware(
       [
          'register_endpoint' => '/_health', // change if needed other endpoint
          'service_id' => '',
          'service' => [
             'upstream' => [
                'type' => 'roundrobin',
                'nodes' => [
                   ':' => 1, // example: books-microservice:80
                ],
             ],
          ],
          'route' => [
             'uri' => "", // example: /books/*
             'service_id' => '', // example: books-microservice
          ],
          'api_admin_secret' => '',
          'api_endpoint' => '', // example: http://api-gateway:9180
        ],
	,
        );
    },
    ......
];
```

add the **Middleware** to `any` route at the end of the routes:

```
use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\APISIXRegisterMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(APISIXRegisterMiddleware::class)
   ->setName('any')
   ;
```

create health check `/_health` or your defined.

welcome, your app will be auto (re)registered in the APISIX on the every health check.

Leader election usage
---------------------

[](#leader-election-usage)

Idea: in the microservice world can be more then one instance who can execute the relevant commands and there is a need for those commands to be executed only by one. Vote for the leader using health check mechanizm!

Balanced with the `APISIXRegisterMiddleware`.

You can use it with:

- Docker health check;
- k8s health check;
- and more others....

Requires `curl`, `docker/k8s` health check mechanism and `ETCD v3`.

Use [DI](https://www.slimframework.com/docs/v4/concepts/di.html) to inject the library Middleware classes:

```
use BenyCode\Slim\Middleware\LeaderElectionMiddleware;

return [
    ......
    LeaderElectionMiddleware::class => function (ContainerInterface $container) {
       return new LeaderElectionMiddleware(
          [
             'leader_election_endpoint' => '/_health', // change if needed other endpoint
             'etcd_endpoint' => '',
             'alection_frequency' => 5, // alection frequence in seconds
              ,
          ],
        );
    },
    ......
];
```

add the **Middleware** to `any` route at the end of the routes:

```
use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\LeaderElectionMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(LeaderElectionMiddleware::class)
   ->setName('any')
   ;
```

get the leader status:

```
protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
   $leader = $request
      ->getAttribute('im_leader')
   ;

   if($leader) {
      // the leader code
   }
}
```

Endpoint protection with X-Api-Token usage
------------------------------------------

[](#endpoint-protection-with-x-api-token-usage)

Idea: Protect you endpoints like health check with Api token.

Use [DI](https://www.slimframework.com/docs/v4/concepts/di.html) to inject the library Middleware classes:

```
use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware;

return [
    ......
    OnePathXApiTokenProtectionMiddleware::class => function (ContainerInterface $container) {
       return new OnePathXApiTokenProtectionMiddleware(
          [
             'path' => '/_health', // change if needed other endpoint
             'x-api-token' => '4bfdb81c03f42600d9018103a4df878b', // change to yours
              ,
          ],
        );
    },
    ......
];
```

add the **Middleware** to `any` route at the end of the routes:

```
use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(OnePathXApiTokenProtectionMiddleware::class)
   ->setName('any')
   ;
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

7

Last Release

840d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb3e8d371d500a222cf7d259a737b7de87389296eb9584360f9a68c594d2a2dc?d=identicon)[benycode](/maintainers/benycode)

---

Top Contributors

[![benycode](https://avatars.githubusercontent.com/u/39025147?v=4)](https://github.com/benycode "benycode (52 commits)")

---

Tags

apisixexception-handlinghealth-checkmiddlewaresettingsslim4middlewarepackageerror-handlerhealth checkslim 4APISIX

### Embed Badge

![Health badge](/badges/benycode-slim-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/benycode-slim-middleware/health.svg)](https://phpackages.com/packages/benycode-slim-middleware)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k239.8M286](/packages/nunomaduro-termwind)[jean85/pretty-package-versions

A library to get pretty versions strings of installed dependencies

1.3k289.5M63](/packages/jean85-pretty-package-versions)[league/uri

URI manipulation library

1.1k206.4M277](/packages/league-uri)[lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

2.3k16.4M48](/packages/lab404-laravel-impersonate)[bmatovu/laravel-xml

Laravel XML Support

91270.4k](/packages/bmatovu-laravel-xml)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
