Developing software 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:

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:

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:

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!

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *