Pragmatism in the real world

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:

Slim3 skeleton screenshot

What’s included?

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.

That’s it

From this point on, you should delete what you don’t want and add what you do in order to write your application!

21 thoughts on “A Slim3 Skeleton

  1. Cool, I was starting out on Slim and even created my own way of directory structure and stuff but this post came about just at the right time :)

    I really like the ability to specify dependencies via constructor, this is extremely useful, thanks for putting this up!

  2. First time with Slim. I've installed skeleton, but I'm not able to read POST or GET variables. It looks like they are not even send.

    I've tried $app->request->post(), $request->post() even $_POST… I've tested using built in server and Apache.

      1. Now, I am study this project:https://github.com/akrabat/slim-bookshelf
        but there error when I run this project, I just begin to use Slimftamework now.
        The file:dependencies.php
        original source:$capsule = $app->capsule; [error]
        if changed to:
        $capsule = $container['capsule']; [OK]

        Can you explained to me, why you write like this:$capsule = $app->capsule;
        if you are right, means what wrong with my setting of my machine.

          1. Rob, Thank you very much for updating the code.

            The project is great, I think.

            But, I want to study slim because it is simple and easy to understand, the url path is a type of viewController in some meanings.

            You introduced VeiwController concept again in this sample project and Database model, this made slim become complex again.

            Maybe slim is the best option to write REST web service.

  3. Guys, thanks for the help, it was stupidity from my side. I've copied a form from another application and that form had hadn't a "name" attributes on input fields… If there wasn't for a junior dev in my team, I would spend days on debugging…

  4. Hi,

    One may be stupid question…

    I set a global var in index.php
    $app->mail = 'me@mysite.com';

    And I can't find anyway to access it in the controller (homeaction.php)…

    Any idea welcome !

    Regards,

    Sylvain

Comments are closed.