First Application

We will create our first slim application, where we will learn setting very simple slim application with popular template view called Twig.
Getting Set Up
Start by making a folder for your project called firstSlim. Inside our firstSlim project add following folder public, bootstrap in root folder. Add two file in public folder.

Inside the bootstrap/ add the following code

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

Where autoload.php will load all the dependencies of our slim applicaiton.

Inside the index.php add the following code to access

require __DIR__ . '/../bootstrap/app.php';


$app = new \Slim\App([ 
    'settings' => [
        'displayErrorDetails' => true,
require __DIR__ . '/../app/routes.php';

Now we can access our public folder as http://localhost/testApp/public/

Step 2: Create routes
Slim doesn’t use any automatic mapping or URL formula so you can make any route pattern you like map onto any function you like, it’s very flexible. Routes can be linked to a particular HTTP verb (such as GET or POST), or more than one verb.
Create new folder app in root folder and add new file routes.php inside

$app->get('/', function($req, $res){
return 'Home';

We have to add httaccess file, .htaccess is short for Hypertext Access is a configuration file for use on web servers running the Apache Web Server software. With .httaccess now we can access

Step 2: Installing Twig template
The Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application.
composer require slim/twig-view

Our resources folder add new folder called views, where we can store all about views. Inside resources/views/home.twig add code
Home Twig

In bootstrap/app.php add the following code to add view dependency to container to the

$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig( __DIR__ . '/../resources/views', [
        'cache' => false,

    $view->addExtension(new \Slim\Views\TwigExtension(

    return $view;

In above code we have first attach our view template to container and we are also creating new slim instance we have to define path where our view are kept. For the production we may keep our cache in different folder.
We can also add optional addExtension to define routes, we need router because we are using link inside our view and we also have to get current uri through request which is the current request of the page.

In app/routes.php we have to add following code to access view page home.twig

$app->get('/home', function($req, $res ){
return $this->view->render($res, 'home.twig');