A preconfigured Silex starter project.

v1.0.2 2016-04-10 22:30 UTC

This package is not auto-updated.

Last update: 2020-04-03 17:36:27 UTC


This is a basic SILEX Skeleton setup the way I personaly prefer it. This project serves as a starting point for new Silex applications.

  • igorw's config-service-provider to load simple PHP array configs
  • dflydev's doctrine-orm-service-provider for easy database access and ORM abstraction
  • symfony's translation module for translations
  • symfony's twig bridge as HTML template language

It also includes a simple demo app that shows you how ...

  • ... to use controllers as seperate classes
  • ... HTML and JSON controllers work
  • ... routing works
  • ... doctrine orm database access works
  • ... internationalization of your app works
  • ... REST works
  • ... exception handling works

Getting started


You must have composer and git installed on your system. Also for initial setup, it is highly recommended that the php executable is in your PATH.

Creating a new project

  1. Open a command line interface
  2. run composer create-project Takuto88/silex-skeleton path/to/install. Composer will create a folder for your and install the new project.
  3. Configure your system and webserver
  4. Assuming you have configured your webserver to silex-demo.dev, navigate to http://silex-demo.dev
  5. Enjoy! You can always read a how-to in order to proceed.

System and webserver config

  1. Setup your webserver's virtual host. See Apache example if you're using apache.
  2. Edit your hosts-file (Windows: C:\Windows\System32\drivers\etc\hosts) (Unix-like systems: /etc/hosts) to include the following line: [your-servername]. If you want your project to be avaliable unter http://silex-demo.dev the line must look like: silex-demo.dev. Be sure that your Webserver's virtual host ServerName listens on that name! See Apache example if you're stuck
  3. Reload your webserver configuration or just restart your webserver.

Apache Example config

Don't blindly copy & paste! READ THE COMMENTS!

In your httpd-vhosts.conf, include the follwing directive:

<VirtualHost *:80>
  ServerName silex-demo.dev # Must match your hosts-file name!
  DocumentRoot "/path/to/your/project/public" # Be sure to point to the /public folder in your project!
  <Directory /path/to/your/project>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All # Not recommended on production systems
    # Apache 2.4 and greater
    Require all granted
    # Apache 2.2: Use this instead
    #Order allow,deny
    #allow from all
  # Optional - Use this to use 'development.php' as your config-file.
  # Silex skeleton will default to 'config.php' if APPLICATION_ENV is undefined.
  # SetEnv APPLICATION_ENV development

  # Log files
  CustomLog "logs/silex-access_log" combined
  ErrorLog "logs/silex-error_log"

Project structure

The most important files and folders are:

  • /app.php: The dependency injection wiring of your app
  • /public: Everything the browser needs to be able to access goes here
    • /public/index.php: The routing config and exception handling
  • /composer.json: Manage your third party dependencies here.
  • /resources: Contains resources for your app
  • /resources/config: Put your application config in here
    • /resources/i18n: Put your language strings here for internationalization
    • /resources/views: Layouts and views go in here
  • /vendor: Composer installs all dependencies like silex in here
  • /src: The source of your application. Contains the SilexSkeleton Demo app.
    • /src/SilexSkeleton/Controller: The Controllers
    • /src/SilexSkeleton/Entity: Database entities go in here. You can use them in your business logic as well as in your views
    • /src/SilxSkeleton/Exception: The exceptions for your app.
    • /src/SilexSkeleton/Service: The businesslogic aka app services. The root folder is used for the interface specs
    • /src/SilexSkeleton/Service/Impl: The implementation of the interfaces

How to

Use a different config-file

Configs are located in /resources/config. Assuming you would like to use local_devconfig.php instead of the default config.php, then do this:

You must set the APPLICATION_ENV environment variable to local_devconfig. This can be done in the following ways:


Add SetEnv APPLICATION_ENV local_devconfig to

  • public/.htaccess OR
  • in your httpd-vhosts.conf

If you go for your vhosts config file, don't forget to reload the webserver configuration or to restart your webserver.

Other webservers

I'm old, I just use Apache as it is good enough for me 😄. Feel free to send a pull request.

Use MySQL instead of SQLite

Look at resources/config.php.dist for an example. For reference, I'll include that here.


return array(
        'debug' => false,
        'db.options' => array(
                'driver' => 'pdo_mysql',
                'dbname' => 'yourdb',
                'host' => '',
                'user' => 'root',
                'password' => 'secret',
                'charset' => 'utf8',
                'port' => '3306',
        'db.orm.options' => array(
                'orm.default_cache' => 'apc'

Then run php vendor/bin/doctrine orm:schema-tool:create in order to have doctrine setup your database automatically for you. Under Windows, use php vendor/doctrine/orm/bin/doctrine.php orm:schema-tool:create instead, as the other one only works on Unix-like systems.

Use a different namespace

It is highly encouraged, that you use namespaces for your projects. Assuming you would like to add the namespace named MyApp:

  • Create a folder called "MyApp" under '/src'
  • If you are going to use doctrine for your database mapping, you must make it aware of your entities. Edit the app.php and look for:
$app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array(
    'orm.proxies_dir' => __DIR__ . "/resources/cache/doctrine/proxy",
    'orm.em.options' => array(
        "mappings" => array(
                'type' => 'annotation',
                'namespace' => 'SilexSkeleton\Entity',
                'path' => __DIR__ . "/src/SilexSkeleton/Entity"

And you're done. Of course you can delete src/SilexSkeleton if you have no more use for it. It simply serves as an example on how things work.

How do I add a route and or controller?

I am assuming we are using the SilexSkeleton namespace for now. This is an example of what's needed:

Controller: /src/SilexSkeleton/Controller/MyNewController.php

namespace SilexSkeleton\Controller;

class MyNewController {
  // Assuming you want to use twig: inject it.
  private $twig;
  public function __construct(\Twig_Environment $twig) {
		$this->twig = $twig;
  public function doMyAction() {
    return $this->twig->render("myaction.html", array());

Template: /resources/views/myaction.html

{% extends "layout.html" %}

{% block content %}
<h1>This is SilexSkeleton\Controller\MyNewController::doMyAction()</h1>
{% endblock %}

Depenendy injection wiring: /app.php

// [...]
$app['controllers.mynewcontoller'] = $app->share(function() use ($app){
	return new SilexSkeleton\Controller\MyNewController($app['twig']);
// [...]

Route: /public/index.php

$app->get('/myaction', 'controllers.mynewcontroller:doMyAction');

And that's all. Go to http://silex-demo.dev/mynewaction (or whatever your VHost is) in order to see what happens.

Add a different language

Just add a new translation file to '/resources/i18n' and name it accordingly. For instance:

  • da.php Would be for Danish.
  • fr.php Would be for French.

You can also add more specific files:

  • de-de.php Would be for German (Germany)
  • de-au.php Would be for German (Austria)

The files itsef must look like this:


return array(
  'messageKey' => 'Localized value to show to the user'

Doctrine ORM pointers

As the database abstraction layer is handled by Doctrine, you should make yourself familiar with the doctrine anotations in order to create your database schema. The annotation documentation will help you out.

Also, the doctrine command line tool is very useful. Just run php vendor/bin/doctrine. That script is broken if you are using Windows. Run php vendor/doctrine/orm/bin/doctrine.php instead and it will work fine. Here is some documentation on the tool itself.

Don't be afraid of Doctrine! It is actually very simple and once you've grasped it, you don't ever want to go back to plain SQL anymore.