{"id":13395601,"url":"https://github.com/relayphp/Relay.Middleware","last_synced_at":"2025-03-13T22:30:33.517Z","repository":{"id":33512604,"uuid":"37158597","full_name":"relayphp/Relay.Middleware","owner":"relayphp","description":"Basic Relay-compatible middleware.","archived":true,"fork":false,"pushed_at":"2018-03-27T19:09:26.000Z","size":54,"stargazers_count":40,"open_issues_count":3,"forks_count":9,"subscribers_count":3,"default_branch":"1.x","last_synced_at":"2024-09-22T18:36:59.090Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/relayphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-09T21:10:21.000Z","updated_at":"2023-12-09T11:21:54.000Z","dependencies_parsed_at":"2022-08-24T13:00:06.894Z","dependency_job_id":null,"html_url":"https://github.com/relayphp/Relay.Middleware","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relayphp%2FRelay.Middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relayphp%2FRelay.Middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relayphp%2FRelay.Middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relayphp%2FRelay.Middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/relayphp","download_url":"https://codeload.github.com/relayphp/Relay.Middleware/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243493035,"owners_count":20299586,"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-07-30T18:00:25.590Z","updated_at":"2025-03-13T22:30:33.046Z","avatar_url":"https://github.com/relayphp.png","language":"PHP","readme":"# Relay Middleware\n\nThis package include the following Relay-compatible middleware:\n\n- _ExceptionHandler_ to handle exceptions from subsequent middleware\n- _FormContentHandler_ to deserialize the URL-encoded payload of a PSR-7 request\n- _JsonContentHandler_ to deserialize the JSON payload of a PSR-7 request\n- _JsonDecoder_ to deserialize the JSON payload of a PSR-7 request (**deprecated**)\n- _ResponseSender_ to send a PSR-7 response\n- _SessionHeadersHandler_ to manage session headers \"manually\", instead of PHP managing them automatically\n- _StatelessExceptionHandler_ to handle exceptions from subsequent middleware (suitable for multiple request/response cycles)\n\nThis package is installable and PSR-4 autoloadable via Composer as `relay/middleware`.\n\n## ExceptionHandler\n\nSimilarly, the _ExceptionHandler_ does what it sound like: it catches any exceptions that bubble up through the subsequent middleware decorators.\n\nThe _ExceptionHandler_ does nothing with the `$request` or `$response`, and passes them directly to `$next` inside a `try/catch` block. If no exception bubbles up, it returns the `$response` from `$next`.  However, if it catches an exception, it returns an entirely new `$response` object with the exception message and an HTTP 500 status code. It then returns the new `$response` object.\n\nThe _ExceptionHandler_ is intended to go near the top of the Relay queue, but after the _ResponseSender_, so that the _ResponseSender_ can then send the returned `$response`.\n\nTo add the _ExceptionHandler_ to your queue, instantiate it directly with an empty $response implementation object ...\n\n```php\n$queue[] = new \\Relay\\Middleware\\ExceptionHandler(new ResponseImplementation());\n```\n\n... or use a `$resolver` of your choice to instantiate it from the `$queue`.\n\n## FormContentHandler\n\n_FormContentHandler_ works almost identically to _JsonContentHandler_ (below), but parses payloads of requests that have `application/x-www-form-urlencoded` as the `Content-Type`.\n\n## JsonContentHandler\n\nAgain, the _JsonContentHandler_ does what it sounds like: it deserializes the JSON\npayload of a PSR-7 request object and makes the parameters available in\nsubsequent middleware decorators.\n\nThe _JsonContentHandler_ checks the incoming request for a method other than `GET`\nand for an `application/json` or `application/vnd.api+json` `Content-Type` header.\nIf it finds both of these, it parses the JSON and makes it available as the\n_parsed body_ of the `$request` before passing it and the `$response` to `$next`.\nIf the method is `GET` or the `Content-Type` header defines a different mime type,\nthe _JsonContentHandler_ ignores the `$request` and continues the chain.\n\nTo add the _JsonContentHandler_ to your queue, instantiate it directly...\n\n```php\n$queue[] = new \\Relay\\Middleware\\JsonContentHandler();\n```\n\n... or use a `$resolver` of your choice to instantiate it from the `$queue`.\n\nTo access the decoded parameters in subsequent middleware, use the\n`getParsedBody()` method of the `$request`\n\n```php\n$decodedJsonData = $request-\u003egetParsedBody();\n```\n\n## JsonDecoder\n\n**NOTE: This handler has been deprecated in favor of _JsonContentHandler_!**\n\nAgain, the _JsonDecoder_ does what it sounds like: it deserializes the JSON\npayload of a PSR-7 request object and makes the parameters available in\nsubsequent middleware decorators.\n\nThe _JsonDecoder_ checks the incoming request for a method other than `GET` and\nfor an `application/json` `Content-Type` header. If it finds both of these, it\ndecodes the JSON and makes it available as the _parsed body_ of the `$request`\nbefore passing it and the `$response` to `$next`. If the method is `GET` or the\n`Content-Type` header does not specify `application/json`, the _JsonDecoder_\ndoes nothing with the `$request` and passes it and the `$response` to `$next`.\n\nTo add the _JsonDecoder_ to your queue, instantiate it directly...\n\n```php\n$queue[] = new \\Relay\\Middleware\\JsonDecoder();\n```\n\n... or use a `$resolver` of your choice to instantiate it from the `$queue`.\n\nTo access the decoded parameters in subsequent middleware, use the\n`getParsedBody()` method of the `$request`\n\n```php\n$decodedJsonData = $request-\u003egetParsedBody();\n```\n\n## ResponseSender\n\nThe _ResponseSender_ does just what it sounds like: it sends the PSR-7 response object.\n\nThe _ResponseSender_ does nothing with the `$request` or `$response`, passing them immediately to `$next`. Afterwards, it takes the returned `$response` and sends it using `header()` and `echo`, and returns the sent `$response`.\n\nThe _ResponseSender_ is intended to go at the top of the Relay queue, so that it is the middleware with the last opportunity to do something with the returned response.\n\nTo add the _ResponseSender_ to your Relay queue, instantiate it directly ...\n\n```php\n$queue[] = new \\Relay\\Middleware\\ResponseSender();\n```\n\n... or use a `$resolver` of your choice to instantiate it from the `$queue`.\n\n## SessionHeadersHandler\n\nNormally, PHP will send out headers for you automatically when you call `session_start()`. However, this means the headers are not being sent as part of the PSR-7 response object, and are thus outside your control. This handler puts them back under your control by placing the relevant headers in the PSR-7 response; its behavior is almost identical to the native PHP automatic session headers behavior.\n\n\u003e NOTE: For this middleware to work, you **must** disable the PHP session header management ini settings. For example:\n\u003e\n\u003e     ini_set('session.use_trans_sid', false);\n\u003e     ini_set('session.use_cookies', false);\n\u003e     ini_set('session.use_only_cookies', true);\n\u003e     ini_set('session.cache_limiter', '');\n\u003e\n\u003e If you do not, the handler will throw a RuntimeException.\n\nTo add the _SessionHeadersHandler_ to your queue, instantiate it directly...\n\n```php\n$queue[] = new \\Relay\\Middleware\\SessionHeadersHandler();\n```\n\n... or use a `$resolver` of your choice to instantiate it from the `$queue`.\n\nWhen instantiating, you can pass a [cache limiter](http://php.net/session_cache_limiter) value as the first constructor parameter. The allowed values are 'nocache', 'public', 'private_no_cache', or 'private'. If you want no cache limiter header at all, pass an empty string ''. The default is 'nocache'.\n\nYou can also pass a [cache expire](http://php.net/session_cache_expire) value, in minutes, as the second constructor parameter. The default is 180 minutes.\n\n## StatelessExceptionHandler\n\nThe _StatelessExceptionHandler_ behaves the same as the _ExceptionHandler_. The difference is that it is suitable for use in environments where it may be used multiple times.\n\nTo add the _StatelessExceptionHandler_ to your queue, instantiate it directly with a factory that can be used to create $response objects ...\n\n```php\n$responseFactory = function () {\n    return new ResponseImplementation();\n};\n\n$queue[] = new \\Relay\\Middleware\\StatelessExceptionHandler($responseFactory);\n```\n","funding_links":[],"categories":["Middleware"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelayphp%2FRelay.Middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frelayphp%2FRelay.Middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelayphp%2FRelay.Middleware/lists"}