PHPackages                             beastbytes/yii-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. [Framework](/categories/framework)
4. /
5. beastbytes/yii-middleware

ActiveLibrary[Framework](/categories/framework)

beastbytes/yii-middleware
=========================

PSR15 middleware classes for Yii3

01PHP

Since Nov 2Pushed 2y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Yii Middleware
==============

[](#yii-middleware)

The package provides middleware classes that implement [PSR-15](https://www.php-fig.org/psr/psr-15/#12-middleware):

- [`AccessChecker`](#accesschecker).
- [`GoBack`](#goback).

For more information on how to use middleware in the [Yii Framework](https://www.yiiframework.com/), see the [Yii middleware guide](https://github.com/yiisoft/docs/blob/master/guide/en/structure/middleware.md).

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

[](#requirements)

- PHP 8.0 or higher.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require --prefer-dist beastbytes/yii-middleware

```

or add

```
"beastbytes/yii-middleware": "^1.0.0"
```

to the `require` section of your composer.json.

General usage
-------------

[](#general-usage)

All classes are separate implementations of [PSR 15](https://github.com/php-fig/http-server-middleware)middleware and don't interact with each other in any way.

### `AccessChecker`

[](#accesschecker)

Checks that the current user has permission to access the current route; use in the route configuration.

```
return [
    Route::get('/') // no checking
         ->action([AppController::class, 'index'])
         ->name('app.index'),

    Route::methods([Method::GET, Method::POST], '/login') // only allow if user is not logged in
         ->middleware(
             fn (AccessChecker $accessChecker) => $accessChecker->withPermission('isGuest')
         )
        ->action([AuthController::class, 'login'])
        ->name('auth.login'),

    Route::get('/my-account') // only allow if user is logged in
         ->middleware(
             fn (AccessChecker $accessChecker) => !$accessChecker->withPermission('isGuest')
         )
        ->action([UserController::class, 'view'])
        ->name('user.view'),

    Route::get('/post/create') // only allow if user create posts
         ->middleware(
             fn (AccessChecker $accessChecker) => !$accessChecker->withPermission('createPost')
         )
        ->action([PostController::class, 'create'])
        ->name('post.create'),
];
```

### `GoBack`

[](#goback)

Stores the current route in the session so that it can be returned to; typically used after a user logs in.

Routes to be ignored - i.e. not stored in the session - can be added so that they do not overwrite the route to go back to; typically the login route is added.

In the router dependency injection configuration:

```
use BeastBytes\Yii\Middleware\GoBack;
use Yiisoft\Config\Config;
use Yiisoft\DataResponse\Middleware\FormatDataResponse;
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollection;
use Yiisoft\Router\RouteCollectionInterface;
use Yiisoft\Router\RouteCollectorInterface;

/** @var Config $config */
/** @var array $params */

return [
    RouteCollectionInterface::class => static function (RouteCollectorInterface $collector) use ($config, $params) {
        $collector
            ->middleware(FormatDataResponse::class)
            ->middleware([
                'class' => GoBack::class,
                'addIgnoredRoutes()' => [
                    $params['user']['loginRoute']
                ]
            ])
            ->addGroup(
                Group::create(null)
                    ->routes(...$config->get('routes'))
            );

        return new RouteCollection($collector);
    },
];
```

In the controller where you want to return to a previous URL

```
use BeastBytes\Yii\Middleware\GoBackMiddleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Status;

    public function __construct(
        private ResponseFactoryInterface $responseFactory
    ) {
    }

    // Actions

    private function goBack(): ResponseInterface
    {
        return $this
            ->responseFactory
            ->createResponse(Status::SEE_OTHER)
            ->withAddedHeader('Location',
                $this
                    ->session
                    ->get(GoBack::URL_PARAM)
            )
        ;
    }
```

Call goBack() from an action.

Testing
-------

[](#testing)

### Unit testing

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```
./vendor/bin/phpunit
```

### Mutation testing

[](#mutation-testing)

The package tests are checked with [Infection](https://infection.github.io/) mutation framework with [Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:

```
./vendor/bin/roave-infection-static-analysis-plugin
```

### Static analysis

[](#static-analysis)

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:

```
./vendor/bin/psalm
```

For license information check the [LICENSE](LICENSE.md) file.

###  Health Score

12

—

LowBetter than 0% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity20

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![beastbytes](https://avatars.githubusercontent.com/u/1470144?v=4)](https://github.com/beastbytes "beastbytes (7 commits)")

### Embed Badge

![Health badge](/badges/beastbytes-yii-middleware/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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