Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Built-in middleware

Several middleware come with Slim itself and need no installation. Three of them are added in almost every application — in the config/middleware.php file.

Body parsing

// config/middleware.php
use Slim\App;
 
return function (App $app): void {
    $app->addBodyParsingMiddleware();
};

This middleware reads the request body and turns it into the array returned by $request->getParsedBody(). It understands JSON, form data and XML — the format is detected from the Content-Type header. Without it a JSON request reaches the action unparsed.

Routing

$app->addRoutingMiddleware();

Routing is the search for the route that matches the request. Slim performs it even without this line, but at the very last moment. The explicit call fixes its place in the chain: middleware added before it already knows which route was found and can build links through RouteContext.

Error handling

$app->addErrorMiddleware(true, true, true);

The error middleware catches any exception and turns it into a response: 404 for an unknown address, 405 for a wrong method, 500 for an error in the code.

ArgumentWhat it enables
FirstShow error details on the page
SecondWrite errors to the log
ThirdWrite the details to the log too

Details on the page are needed only during development, so the first argument is taken from the settings.

Everything together

The config/middleware.php file of the blog project looks like this:

// config/middleware.php
use App\Middleware\ResponseTimeMiddleware;
use Slim\App;
 
return function (App $app): void {
    $settings = $app->getContainer()->get('settings');
 
    $app->add(ResponseTimeMiddleware::class);
 
    $app->addBodyParsingMiddleware();
    $app->addRoutingMiddleware();
    $app->addErrorMiddleware($settings['debug'], true, true);
};

The order matters. The middleware added last runs first, so the error middleware stands at the end of the file: it wraps all other layers and catches exceptions from any of them. Your own middleware is added above the three built-in ones.

Checking

Open an address that does not exist, for example http://localhost:8080/nothing. With APP_DEBUG=true a "404 Not Found" page with details appears. Change the value in .env to false — only a short message remains.

Other built-in middleware

ClassWhat it does
Slim\Middleware\MethodOverrideMiddlewareLets a form send PUT or DELETE through a hidden _METHOD field
Slim\Middleware\ContentLengthMiddlewareAdds a Content-Length header to the response

They are attached with $app->add(new MethodOverrideMiddleware()). The method override middleware is added after addRoutingMiddleware(): the method must be replaced before the route is looked up.

Common mistake

If the error middleware is not added last, it will not catch exceptions from layers added after it: the visitor sees a blank page or a raw PHP error. Write new middleware above the addErrorMiddleware() line, not at the end of the file.

Thanks for reading!

Donate to the site — we work hard for you.