The Missing Slimframework v4 Docs

netscape101
DevOpsOnTheBlock
Published in
3 min readAug 6, 2019
From here: https://camo.githubusercontent.com/d9580c2070429070a7f0d4bdfd628b88dbad0385/687474703a2f2f692e696d6775722e636f6d2f49734d79544a502e6a7067

At work we use a framework called “Slimframework” which is a really cool framework in PHP that doesn’t do that much, but that’s kind of the main selling point of this framework.

Recently Slimframework version 4 was released and I had to start digging into the docs to try and figure out how to upgrade one of my applications from slim framework version 3.

This article is part 1 of my series: “ The Missing Slimframework v3 Docs”.

Maintaining Behavior in you code written for Slim framework v3:

Perhaps you made use of the following setting in your settings.php when Slim version 3 was still a thing.

<?php
return [
'settings' => [
...
"determineRouteBeforeAppMiddleware" => true,
...

You will need to do the following to maintain that same behavior in Slim version 4:
“If you were using determineRouteBeforeAppMiddleware, you need to add the Middleware\RoutingMiddleware middleware to your application just before your call run() to maintain the previous behaviour.”
That is mentioned here: http://www.slimframework.com/docs/v4/middleware/routing.html

What is route middleware?

From here: https://www.mememaker.net/api/bucket?path=static/img/memes/full/2014/Aug/27/20/huh-im-so-confused.jpg

Middleware can be attached to a route or to a route group. This is a new feature in v4. You can read about it here: http://slim-website.lgse.com/docs/v4/objects/routing.html#route-middleware

Here are some examples how to actually do this here: http://slim-website.lgse.com/docs/v4/concepts/middleware.html

Here is a good example:

...
$app = AppFactory::create();
// Add Middleware On App
$app->add(new ExampleMiddleware());
// Add Middleware On Route
$app->get('/', function () { ... })->add(new ExampleMiddleware());
// Add Middleware On Group
$app->group('/', function () { ... })->add(new ExampleMiddleware());
...

How does routing work in version 4 of the slim framework?

According to this page: http://www.slimframework.com/docs/v4/middleware/routing.html

“The routing has been implemented as middleware. We are still using FastRoute as the default router but it is not tightly coupled to it. If you wanted to implement another routing library you could by creating your own implementations of the routing interfaces.”

On another page in the docs the following is mentioned:
“The Slim Framework’s router is built on top of the Fast Route component, and it is remarkably fast and stable. While we are using this component to do all our routing, the app’s core has been entirely decoupled from it and interfaces have been put in place to pave the way for using other routing libraries.” http://dev.slimframework.com/docs/v4/objects/routing.html

Confused?

Yeah I’m also kind of confused right now and looking over all the docs for v4 I can’t find the same kind of detailed examples for creating an application like they had for v3. I will try make my own docs and post them on this blog for other lost souls in the same position as I am in.

--

--