PHPackages                             zakirullin/conditional-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. [HTTP &amp; Networking](/categories/http)
4. /
5. zakirullin/conditional-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

zakirullin/conditional-middleware
=================================

PSR-15 middleware that makes it possible to add conditional middlewares, based on request

0.1.5(7y ago)22951MITPHPPHP ^7.1

Since May 14Pushed 7y ago1 watchersCompare

[ Source](https://github.com/zakirullin/conditional-middleware)[ Packagist](https://packagist.org/packages/zakirullin/conditional-middleware)[ Docs](https://github.com/zakirullin/conditional-middleware)[ RSS](/packages/zakirullin-conditional-middleware/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (6)Versions (6)Used By (0)

zakirullin/conditional-middleware
=================================

[](#zakirullinconditional-middleware)

[![Build Status](https://camo.githubusercontent.com/d916ac5ccb350c25e6242d1d87afd20e0c6dfc3a6a33328ad21b3fe7a2efd4f6/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/zakirullin/conditional-middleware)[![Scrutinizer](https://camo.githubusercontent.com/d9c35065b5e586af6ced6d6720a4c5e4947e52cb0858e3e21486946b3ac87960/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/zakirullin/conditional-middleware/)[![PHP from Packagist](https://camo.githubusercontent.com/7bc9de6e687bfe44b3ea3d8dcc59d5edee50e4770cedfc1912a3de876fd69ce6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/7bc9de6e687bfe44b3ea3d8dcc59d5edee50e4770cedfc1912a3de876fd69ce6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)[![GitHub commits](https://camo.githubusercontent.com/f62f2f34316b4c79fdc1bd8388b94bb67abf3a3f94cdc55d44d8f4d4466f22bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652f302e312e302e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/f62f2f34316b4c79fdc1bd8388b94bb67abf3a3f94cdc55d44d8f4d4466f22bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f7a616b6972756c6c696e2f636f6e646974696f6e616c2d6d6964646c65776172652f302e312e302e7376673f7374796c653d666c61742d737175617265)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

PSR-15 middleware that makes it possible to add conditional middlewares, based on `request`

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1
- A [PSR-7](https://packagist.org/providers/psr/http-message-implementation) http message implementation ([Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), etc...)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

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

[](#installation)

This package is installable and autoloadable via Composer as [zakirullin/conditional-middleware](https://packagist.org/packages/zakirullin/conditional-middleware).

```
composer require zakirullin/conditional-middleware
```

PHP
---

[](#php)

```
$shouldProtect = function (\Psr\Http\Message\ServerRequestInterface $request) {
    $handler = $request->getAttribute('handler');
    return $handler != 'login';
};
$getIdentity = function (\Psr\Http\Message\ServerRequestInterface $request) {
    $session = $request->getAttribute('session');
    return [$session->get('userId')];
};

$dispatcher = new Dispatcher([
    ...
    new \Zakirullin\Middlewares\CSRF($shouldProtect, $getIdentity, 'secret'),
    ...
]);
```

Options
-------

[](#options)

```
__construct(
    callable $shouldProtect,
    callable $getIdentity,
    string $secret,
    string $attribute = self::ATTRIBUTE,
    int $ttl = self::TTL,
    string $algorithm = self::ALGORITHM
)
```

#### `name(string $name)`

[](#namestring-name)

The session name. If it's not defined, the default `PHPSESSID` will be used.

---

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

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 ~86 days

Total

5

Last Release

2572d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/44e326ab3bffd640b622d3cecba8f5fc99bc0ec657ae59f841e871dbdead08ac?d=identicon)[zakirullin](/maintainers/zakirullin)

---

Top Contributors

[![zakirullin](https://avatars.githubusercontent.com/u/3357383?v=4)](https://github.com/zakirullin "zakirullin (5 commits)")

---

Tags

middlewaremiddleware-pipelinepsr-15psr-7httppsr-7middlewareserverpsr-15optionalconditional

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/zakirullin-conditional-middleware/health.svg)

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

###  Alternatives

[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)[middlewares/http-authentication

Middleware to implement Basic and Digest Http authentication

35302.0k2](/packages/middlewares-http-authentication)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)

PHPackages © 2026

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