Pragmatism in the real world

Slim4-empty: minimal Slim 4 starting point

To simplify creating a new Slim 4 project, I’ve created slim4-empty which does this for me.

To use it:

$ composer create-project akrabat/slim4-empty my-new-project

and you’re done!

The my-new-project directory is created and contains Slim 4 along with a minimally viable public/index.php to get you going. You can then run it with the PHP built-in server:

php -S 0.0.0.0:8888 -t public/

And navigate to https://localhost:8888 to view “Hello World” in your browser.

What does it contain?

Slim4-empty provides no structure to your project and adds no extraneous components at all. You do the rest. It simply contains the following:

Slim & Slim-Psr7

The composer create-project command will create a new directory (my-new_project) and then within it will install Slim and Slim-Psr7. Of course with Slim 4, you are free to use any PSR-7 component, but I happen to like our one :)

public/index.php

Within the public directory, slim4-empty provides a minimal index.php:

<?php

declare(strict_types=1);

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

// Create app
$app = AppFactory::create();

// Register middleware
$app->addRoutingMiddleware();
$app->addBodyParsingMiddleware();
$app->addErrorMiddleware(true, true, true);

// Register routes
$app->get('/[{name}]', function (
    ServerRequestInterface $request,
    ResponseInterface $response,
    array $args
): ResponseInterface {
    $name = $args['name'] ?? 'world';
    $response->getBody()->write("hello $name");
    return $response;
});

// Run app
$app->run();

As you can see from the comments, it does the following:

  • Create the App instance – The AppFactory does all the hard work here for us by finding the PSR-7 component and registering it.
  • Register a minimal set of middleware – Slim’s default error handling is flexible and you should use it. The body parsing middleware is mostly useful for APIs, but doesn’t impact performance for standard websites. The routing middleware is required to route from a URL to a handler. If it’s missing, it’ll be added by slim, but this is a case where I prefer to be explicit.
  • Add a route for `/` – Nothing works without a route! This is a minimal one that you can change to suite your needs.
  • Run the application – This will run the middleware stack and then run the route handler and send the response back to the client (web browser).

Who can remember all that? I have to go and copy/paste it, which is why I wrote slim4-empty!

That’s it

Maybe it’s useful to someone else other than me; I’m certainly finding it helpful!

2 thoughts on “Slim4-empty: minimal Slim 4 starting point

  1. Thanks for this, Rob. During the first days after v4 release there was much hue and cry about the decoupled-and-flexible-to-the-max-architecture, so was my impression. Your “empty slim” setup will take away their fears :-)

Comments are closed.