Skip to main content
Version: 4.x

HTTP Middleware in BowPHP

Introduction​

Middleware provides a convenient mechanism for filtering HTTP requests entering your application. For example, you can write a middleware to check whether a user is logged in and then act accordingly. You can run code before and after your Bow application to manipulate the Request and Response objects however you like.

How it works​

Different frameworks use the middleware system in different ways. Bow adds middleware as a queue on top of your main application. Each new middleware layer is added to the existing middleware queue. The queue structure expands outward as additional intermediate layers are added.

When you run the Bow application, Request objects travel through the middleware structure from the outside in. They first enter the outermost middleware, then the next outermost middleware (and so on), until they reach the application itself. Once the Bow application has dispatched the appropriate route, the resulting Response object leaves the Bow application and is serialized into an HTTP response that is returned to the HTTP client.

The only strict requirement is that a middleware must return a value other than null. Each middleware must call the next middleware and pass the Request objects to it as arguments via the $next method.

Bow includes several middleware by default, such as the csrf and auth middleware.

Adding a middleware​

To add a middleware, use the add:middleware command from php bow:

php bow add:middleware IpMiddleware

Note that all middleware is saved by default in the app/Middleware file.

So the middleware we just added will check the client's IP address, and if the address is equal to 127.0.0.1 we will say the application is in DEV MODE.

But first, let's look at the contents of the IpMiddleware file. The process method is what runs the middleware, and the callable allows the next middleware to be run.

namespace App\Middlewares;

use Bow\Http\Request;

class IpMiddleware
{
/**
* Middleware entry point.
*
* @param Request $request
* @param callable $next
*/
public function process(Request $request, callable $next)
{
//
return $next($request);
}
}

For our example, we would then have the following process:

public function process(Request $request, callable $next)
{
if ($request->ip() == '127.0.0.1') {
return "MODE DEV";
}

return $next($request);
}

Registering a middleware​

Registration is generally done in the app/Kernel.php file with a key that identifies it.

public function middlewares()
{
return [
...
'ip' => \App\Middlewares\IpMiddleware::class
...
];
}

It is also possible to add middleware globally across the application. This is done in the routes file using the middleware method on $app, which returns an instance of \Bow\Router\Router::class, so all routes defined with this instance will inherit the specified middleware.

$router = $app->middleware(\App\Middlewares\IpMiddleware::class);
$router->get('/', 'HomeController::index');

// Or
$router = $app->middleware([
\App\Middlewares\IpMiddleware::class,
\App\Middlewares\OtherMiddleware::class,
]);
$router->get('/', 'HomeController::index');

Using the middleware​

After defining a middleware in app/Kernel.php.

$app->get('/', 'HomeController::index')->middleware('ip');
$app->get('/', 'HomeController::index')->middleware(['ip', 'autre']);

// or
$app->route([
'path' => '/',
'method' => 'GET',
'handler' => 'HomeController::index',
'middleware' => ['ip', 'autre']
]);

Is something missing?

If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.