Repository navigation
Creating Middleware
Wes Rice edited this page Mar 21, 2016
·
3 revisions
Each middleware for Http Messages is installed by a separate Craft plugin. This plugin is responsible for registering the middleware handle and the middleware class.
Registering the middleware handle lets the Http Message plugin know which middleware config to pull from the stack of middleware defined for a matched route.
/**
* Register Http Messages Middleware Handle
*
* @return string Middleware Handle
*/
public function registerHttpMessagesMiddlewareHandle()
{
return 'myMiddleware';
}/**
* Register Http Messages Middleware Class
*
* @return string Middleware Class
*/
public function registerHttpMessagesMiddlewareClass()
{
return '\\MyNamespace\\Middleware\\MyMiddleware';
}To create a middleware, create a class using the basic template below and ensure that this class gets some somehow, usually in a via your custom middleware plugin class.
Remember that a middleware has two responsibilities:
- Call the next middleware, passing the Request and Response objects to it.
- Return a Response object.
<?php
namespace MyNamespace\Middleware;
use HttpMessages\Http\CraftRequest as Request;
use HttpMessages\Http\CraftResponse as Response;
class MyMiddleware
{
/**
* Invoke
*
* @return Response Response
*/
public function __invoke(Request $request, Response $response, callable $next)
{
// Do something
$response = $next($request, $response);
// Do something
return $response;
}
}- [Requirements] (https://github.com/airtype/httpmessages/wiki/Requirements)
- [Installing and Updating] (https://github.com/airtype/httpmessages/wiki/Installing-and-Updating)
- [Core Concepts] (https://github.com/airtype/httpmessages/wiki/Core-Concepts)
- [PSR-7 Messages] (https://github.com/airtype/httpmessages/wiki/PSR-7-Messages)
- [Middleware] (https://github.com/airtype/httpmessages/wiki/Middleware)
- [Routing] (https://github.com/airtype/httpmessages/wiki/Routing)
- [Configuration] (https://github.com/airtype/httpmessages/wiki/Configuration)
- [Creating Middleware] (https://github.com/airtype/httpmessage/wiki/Creating-Middleware)