{"id":22640678,"url":"https://github.com/beastbytes/yii-middleware","last_synced_at":"2025-03-29T05:25:35.614Z","repository":{"id":204743976,"uuid":"712528085","full_name":"beastbytes/yii-middleware","owner":"beastbytes","description":"Collection of PSR15 middlewares for Yii3","archived":false,"fork":false,"pushed_at":"2023-11-02T23:30:00.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-03T15:55:00.699Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beastbytes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-31T16:38:37.000Z","updated_at":"2023-10-31T17:59:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ba7d474-6505-4113-b6e2-5658814b03e3","html_url":"https://github.com/beastbytes/yii-middleware","commit_stats":null,"previous_names":["beastbytes/yii-middleware"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fyii-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fyii-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fyii-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fyii-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beastbytes","download_url":"https://codeload.github.com/beastbytes/yii-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246143320,"owners_count":20730241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-09T04:13:07.957Z","updated_at":"2025-03-29T05:25:35.586Z","avatar_url":"https://github.com/beastbytes.png","language":"PHP","readme":"# Yii Middleware\nThe package provides middleware classes that implement [PSR-15](https://www.php-fig.org/psr/psr-15/#12-middleware):\n\n- [`AccessChecker`](#accesschecker).\n- [`GoBack`](#goback).\n\nFor more information on how to use middleware in the [Yii Framework](https://www.yiiframework.com/), see the\n[Yii middleware guide](https://github.com/yiisoft/docs/blob/master/guide/en/structure/middleware.md).\n\n## Requirements\n- PHP 8.0 or higher.\n\n## Installation\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\ncomposer require --prefer-dist beastbytes/yii-middleware\n```\n\nor add\n\n```json\n\"beastbytes/yii-middleware\": \"^1.0.0\"\n```\n\nto the `require` section of your composer.json.\n\n\n## General usage\n\nAll classes are separate implementations of [PSR 15](https://github.com/php-fig/http-server-middleware)\nmiddleware and don't interact with each other in any way.\n\n### `AccessChecker`\n\nChecks that the current user has permission to access the current route; use in the route configuration.\n\n```php\n\nreturn [\n    Route::get('/') // no checking\n         -\u003eaction([AppController::class, 'index'])\n         -\u003ename('app.index'),\n         \n    Route::methods([Method::GET, Method::POST], '/login') // only allow if user is not logged in\n         -\u003emiddleware(\n             fn (AccessChecker $accessChecker) =\u003e $accessChecker-\u003ewithPermission('isGuest')\n         )\n        -\u003eaction([AuthController::class, 'login'])\n        -\u003ename('auth.login'),\n        \n    Route::get('/my-account') // only allow if user is logged in\n         -\u003emiddleware(\n             fn (AccessChecker $accessChecker) =\u003e !$accessChecker-\u003ewithPermission('isGuest')\n         )\n        -\u003eaction([UserController::class, 'view'])\n        -\u003ename('user.view'),\n        \n    Route::get('/post/create') // only allow if user create posts\n         -\u003emiddleware(\n             fn (AccessChecker $accessChecker) =\u003e !$accessChecker-\u003ewithPermission('createPost')\n         )\n        -\u003eaction([PostController::class, 'create'])\n        -\u003ename('post.create'),\n];\n```\n\n### `GoBack`\nStores the current route in the session so that it can be returned to; typically used after a user logs in.\n\nRoutes to be ignored - i.e. not stored in the session - can be added so that they do not overwrite the route to go \nback to; typically the login route is added.\n\nIn the router dependency injection configuration:\n\n```php\nuse BeastBytes\\Yii\\Middleware\\GoBack;\nuse Yiisoft\\Config\\Config;\nuse Yiisoft\\DataResponse\\Middleware\\FormatDataResponse;\nuse Yiisoft\\Router\\Group;\nuse Yiisoft\\Router\\RouteCollection;\nuse Yiisoft\\Router\\RouteCollectionInterface;\nuse Yiisoft\\Router\\RouteCollectorInterface;\n\n/** @var Config $config */\n/** @var array $params */\n\nreturn [\n    RouteCollectionInterface::class =\u003e static function (RouteCollectorInterface $collector) use ($config, $params) {\n        $collector\n            -\u003emiddleware(FormatDataResponse::class)\n            -\u003emiddleware([\n                'class' =\u003e GoBack::class,\n                'addIgnoredRoutes()' =\u003e [\n                    $params['user']['loginRoute']\n                ]\n            ])\n            -\u003eaddGroup(\n                Group::create(null)\n                    -\u003eroutes(...$config-\u003eget('routes'))\n            );\n\n        return new RouteCollection($collector);\n    },\n];\n```\n\nIn the controller where you want to return to a previous URL\n\n```php\nuse BeastBytes\\Yii\\Middleware\\GoBackMiddleware;\nuse Psr\\Http\\Message\\ResponseFactoryInterface;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Yiisoft\\Http\\Status;\n\n    public function __construct(\n        private ResponseFactoryInterface $responseFactory\n    ) {\n    }\n    \n    // Actions\n\n    private function goBack(): ResponseInterface\n    {\n        return $this\n            -\u003eresponseFactory\n            -\u003ecreateResponse(Status::SEE_OTHER)\n            -\u003ewithAddedHeader('Location', \n                $this\n                    -\u003esession\n                    -\u003eget(GoBack::URL_PARAM)\n            )\n        ;\n    }\n```\n\nCall goBack() from an action.\n\n## Testing\n### Unit testing\nThe package is tested with [PHPUnit](https://phpunit.de/). To run tests:\n\n```shell\n./vendor/bin/phpunit\n```\n\n### Mutation testing\nThe package tests are checked with [Infection](https://infection.github.io/) mutation framework with\n[Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:\n\n```shell\n./vendor/bin/roave-infection-static-analysis-plugin\n```\n\n### Static analysis\nThe code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:\n\n```shell\n./vendor/bin/psalm\n```\n\nFor license information check the [LICENSE](LICENSE.md) file.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeastbytes%2Fyii-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeastbytes%2Fyii-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeastbytes%2Fyii-middleware/lists"}