Making a CLI Request in Slim 3

I’m using @akrabat’s Slim 3 skeleton for the PHPMinds website, and I was looking for a way to make a Request through the CLI to use for crons and workers. But I wanted to call it the same way as a normal request.

php public/index.php /status GET event=true

DVO Media had a nice way of doing it in index.php:

With Slim 3 using middleware, I thought that may have been a tidier way to do it, as I don’t like to add any logic in my index.php

I thought initially I would add it to the route. That didn’t work. It was too late in the request cycle.

I checked on irc (#slimphp) and @akrabat advised me to put it in the Middleware section. That did feel a much cleaner way to do it, and it worked!

In the middleware section (middleware.php): $app->add(new App\Middleware\CliRequest());

And the Middleware itself: