silex Getting started with silex

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


This section provides an overview of what silex is, and why a developer might want to use it.

It should also mention any large subjects within silex, and link out to the related topics. Since the Documentation for silex is new, you may need to create initial versions of those related topics.

Hello world

Create web directory in same folder as the vendor directory. Create index.php file in web directory with contents

// web/index.php

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

$app = new Silex\Application();

$app->get("/", function () {
    return "Hello world!";

$app->get("/hello/{name}", function ($name) use ($app) {
    return "Hello ".$app->escape($name);


To start app using PHP built-in server run

php -S localhost:8080 -t web

Now you can open the browser and navigate to http://localhost:8080 , to see

Hello World!

We also defined one dynamic route. Navigate to http://localhost:8080/hello/<YOUR_NAME> replacing <YOUR_NAME> with your own name to be greeted by your first Silex app.

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]

Got any silex Question?