Slim 3 Framework Tutorial: Download, Setup, Create Routes and Template

If you are a PHP developer for some time now, then you already heard about Slim framework. Slim is mainly popular for building APIs. Reason is its routing feature but you can also develop dynamic websites and systems using slim.

As per official definition:

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Features of Slim Framework:

There are four main features of slim.

  • HTTP Router: Slim provides a fast and powerful router that maps route callbacks to specific HTTP request methods and URIs. It supports parameters and pattern matching.
  • Middleware: Build your application with concentric middleware to tweak the HTTP request and response objects around your Slim app
  • PSR-7 Support: Slim supports any PSR-7 HTTP message implementation so you may inspect and manipulate HTTP message method, status, URI, headers, cookies, and body
  • Dependency Injection: Slim supports dependency injection so you have complete control of your external tools. Use any Container-Interop container.

I am not going to cover API in this tutorial. I will do that in my upcoming tutorials. But right now I am going to develop a website using slim 3 framework.

This will be the series of tutorials because I cannot cover all in one tutorial. But I can share you what I will going to do in this tutorial.

Slim 3 Framework Tutorial:

  • Download Slim 3 via composer
  • Add PHP-View via composer for templating. Because slim does not have a view layer
  • Create Src folder and put routes and settings there.
  • Create index.php on folder root.
  • Create Templates folder and put homepage, about, 404 and layouts structure.
  • Create Assets folder and put all css and js there.

If you don’t have composer on your machine. Then you can check my post How to install Composer on Windows with XAMPP.

I am using xampp for my local development so I am going to create folder in htdocs as slimnew. Now open command prompt (ctrl +r and type cmd) go to slimnew folder under htdocs and type below command.

slim 3 installation composer command

When you hit enter then slim installation will start and you will see below messages.

slim 3 installation composer

Slim 3 initial Folder Structure:

By default installation create only vendor folder and composer .json and composer.lock file. Just like below

slim 3 installed directory

Download Views and Templates:

Slim does not have a view layer by default because slim view is the http response. But slim provides the Twig-View and PHP-View components to render templates.

I am going to use PHP-View for templating. Type below command under htdocs/slimnew

slim 3 install php view

slim 3 php view installation

Create Src Folder:

Create Settings.php:

Now create src folder under slimnew and create file settings.php and put below code in settings.php

Above code is just an array with settings index as first and displayErrorDetails, addContentLengthHeader and db indexes as second with their respective values. I will cover database in my next tutorial.

Create routes.php

Slim provides 8 routes for handling HTTP request. We will cover them according to the requirement of this tutorial. But if you want to look them. You can visit Slim Router.

In the above routes.php code:

  • $app->get() : handles only GET HTTP request and it takes 2 arguments. The route pattern and the route callback.
  • First $app->get has only take “/” so this means this route is for home page. $data is an array having main_heading and sub_heading keys.
  • $this->view->render : has 3 parameters one is $repsonse, second is template and third is argument pass in template file.
  • Second $app->get takes “/about” which means if slimnew/about will land on this route.
  • Homepage.php and about.php pages will be placed in templates folder.

Create index.php

Create index.php on folder root htdocs/slimnew/index.php. Paste below code.

In the above of index.php on top we used:

  • Use \Psr\Http\Message\ServerRequestInterface as Request : Object has all the server parameter.
  • Use \Psr\Http\Message\ResponseInterface as Response : Object has response status, headers and content type
  • require('vendor/autoload.php'): is loading all slim libararies
  • $settings = require __DIR__ . '/src/settings.php' : just include settings.
  • $app = new \Slim\App($settings) : pass settings array in $app object.
  • $container = $app->getContainer() : get slim container.
  • $container['view']: register PHP-View as a view container which will render layout of templates folder.
  • $container['notFoundHandler']: is used to register 404 page.
  • require __DIR__ . '/src/routes.php' : include route file for website routing.
  • $app->run() : run slim app.

Create Templates Folder:


In the above code we print $main_heading as main heading and $sub_heading as paragraph. Both $main_heading and $sub_heading are passed as an argument in src/routes.php file.

Slim 3 Framework homepage


Slim 3 Framework about us

Create layouts folder:

In templates/layouts folder create 2 files header.php and footer.php


Above is the header code which include css files and render top navigation. $baseUrl is a variable which we set in index.php in view container.


Assets Folder:

In this folder we put js and css files.

slim 3 assets

Final Slimnew Folder Structure:

slim 3 final folder structure

All things are done now it’s time to check slimnew in browser. We have 2 options for open site in browser first one is goto browser and type slimnew url (for me it is localhost:8080/slimnew) and second method is goto command prompt xampp/htdocs/slimnew and type php -S localhost:9000 and then goto browser and type localhost:9000.

If you are using second option then must change baseUrl in index.php right now it is set to /slimnew/ you have to change this with "/".

Second tutorial on this series is Slim 3 Framework Tutorial: Create, Configure, Load and Access Database


3 Replies to “Slim 3 Framework Tutorial: Download, Setup, Create Routes and Template”

  1. I’ve just started learning Slim Framework and i’ve found you’r way is the simplest one. Great Job. If by any chance you’ve integrated with database as well please let me know. Thank you so much

Leave a Reply

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