silex Getting started with silex Installation or Setup


Consider following directory structure to comply with best practices:

    |    |---[resources]
    |    routes.php
    |    |---[resources]
    |    |    |---[css]
    |    |    |---[img]
    |    |    |---[js]
    |    .htaccess
    |    index.php

Composer is the most flexible way to get started with Silex. Create a directory to host your Silex application (Named ProjectRoot above), cd to that directory and run the following command to create a composer.json file:

composer require silex/silex "~2.0"

This will add some files and directories into vendor directory, under ProjectRoot.

After that, all you need to do is require the vendor/autoload.php file and create an instance of Silex\Application in your index.php file under ProjectRoot/web. After your controller definitions (routes.php), call the run method on your application:


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

$app = new Silex\Application();
require_once __DIR__ . '/../app/routes.php';



$app->get("/", function () {
    return new \Symfony\Component\HttpFoundation\Response('Hello World!');


<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]