A Slim3 Skeleton
Warning Slim 3 is currently under active development. Expect BC breaks!
I’m creating a number of projects in Slim 3 (which is currently under development) at the moment in order test things out and found that I was setting them up in essentially the same way. To save me having to do this each time, I’ve now created a skeleton project that is set up with the directory structure that I like and also includes Twig and Monolog.
To create a new Slim 3 project, simply do this:
$ composer create-project -n -s dev akrabat/slim3-skeleton my-app
This will create a new folder called my-app with the project files in it and then install the composer dependencies. You now have a working skeleton application, so test it using the built-in PHP web server:
$ cd my-app
$ php -S 0.0.0.0:8888 -t public public/index.php
Browse to http://localhost:8888 and you should see this:
When exploring the application, these are the key files to look at.
- index.php instantiates the Slim application object, sets the application up using files in app/ and runs it.
- app/dependencies.php contains all the dependencies that are registered with Pimple.
- app/middleware.php is where you should register any application middleware that you want to use. It’s empty in the skeleton.
- app/routes.php contains all the routes that the application responds to. I like having them all in one place and to keep the file sensible, I use the DIC to grab the action for dispatch.
- app/src/Action/HomeAction.php is the class that is loaded by the DIC and then the dispatch method is executed. The nice thing about having a class like this is that I can load the dependencies via the constructor. Look in dependencies.php for the factory for this class.
- app/templates/home.twig is the Twig view script that is rendered.
From this point on, you should delete what you don’t want and add what you do in order to write your application!