Converting A Silex Project to A Symfony Project

Peter Lafferty
5 min readFeb 28, 2018

--

This article covers converting the Silex skeleton application to the Symfony Skeleton application. They are both simple projects but the steps should help when converting larger projects.

The first step is to create both skeletons, the Symfony skeleton will be used to copy some configuration files:

composer create-project fabpot/silex-skeleton silextosymfony "~2.0"composer create-project symfony/skeleton skeleton

Note that create-project also runs composer install so you should see a vendor directory in both directories. Change directory in to the silextosymfony project, initialise the git repository and add some files in to the repo. This is to make it easier to see changes as we go along.

cd silextosymfony
git init
git add .
git diff --cached
git commit -m "initial commit"

Verify that the Silex project works. This can be done by running php -S localhost:8080 -t public. You should see a message that says “Welcome to your new Silex Application!” in your web browser.

A couple of things to note about this Silex application:

  • ./config has the configuration files for dev and prod
  • ./src has the application code
  • PSR-0 is used
  • ./templates contains Twig templates
  • ./web contains the index.php and css files
  • one route is defined in .src/controllers.php
  • logs are stored in ./var/logs and cache files are in ./var/cache

Next step is to copy across some basic config needed by Symfony:

rm -rf vendor #makes life easier
cp ../skeleton/config/services.yaml config/services.yaml
cp ../skeleton/composer.json composer.json
cp ../skeleton/bin/console bin/console
git add bin/console config/services.yaml composer.json

Note that the Symfony composer.json is using a higher version of PHP and has a PSR-4 namespace defined.

Lets remove some Silex specific items:

git rm src/app.php #will be replaced with src/Kernel.php
git rm src/console.php #not going to cover here
git rm src/controllers.php #will create a specific route
git rm config/dev.php
git rm config/prod.php
git rm -r var/logs/
git rm web/index.php
git rm web/index_dev.php
git mv web public #route directory is public instead of web
git commit -m "preparation for symfony 4"
rm composer.lock #don't remove from git

Removing the composer.lock will allow a fresh install when running composer. After doing all that the directory structure should look like this:

.
├── LICENSE
├── README.rst
├── bin
│ └── console
├── composer.json
├── config
│ └── services.yaml
├── phpunit.xml.dist
├── public
│ └── css
│ └── main.css
├── templates
│ ├── errors
│ │ ├── 404.html.twig
│ │ ├── 4xx.html.twig
│ │ ├── 500.html.twig
│ │ ├── 5xx.html.twig
│ │ └── default.html.twig
│ ├── index.html.twig
│ └── layout.html.twig
├── tests
│ └── controllersTest.php
└── var
└── cache

Now run composer install and the directory structure should change to something like this:

.
├── LICENSE
├── README.rst
├── bin
│ └── console
├── composer.json
├── composer.lock
├── config
│ ├── bundles.php
│ ├── packages
│ │ ├── dev
│ │ │ └── routing.yaml
│ │ ├── framework.yaml
│ │ ├── routing.yaml
│ │ └── test
│ │ └── framework.yaml
│ ├── routes.yaml
│ └── services.yaml
├── phpunit.xml.dist
├── public
│ ├── css
│ │ └── main.css
│ └── index.php
├── src
│ ├── Controller
│ └── Kernel.php
├── symfony.lock
├── templates
│ ├── errors
│ │ ├── 404.html.twig
│ │ ├── 4xx.html.twig
│ │ ├── 500.html.twig
│ │ ├── 5xx.html.twig
│ │ └── default.html.twig
│ ├── index.html.twig
│ └── layout.html.twig
├── tests
│ └── controllersTest.php
├── var
│ ├── cache
│ └── log
└── vendor

Note that Flex has generated a couple of files and directories for you. The most important things to note are ./src/Kernel.php, ./web/index.php and everything in ./config. ./src/Kernel.php is the Symfony version of the Silex Application class.

Running git status will give an output similar to this:

On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
modified: composer.lock
Untracked files:
(use "git add <file>..." to include in what will be committed)
.env.dist
config/bundles.php
config/packages/
config/routes.yaml
public/index.php
src/
symfony.lock

Add the newly generated files to git:

git add .gitignore composer.lock .env.dist config/ public/ src/ symfony.lock
git commit -m "result after composer install"

Then add some bundle dependencies:

composer require twig
composer require logger #adds in monolog
composer require asset #for css etcetera
git add .
git diff --cached
git commit -m "adding in twig, monolog and asset bundles"

The final step is to create a controller for the index ./src/Controller/IndexController.php and to update the route configuration. The controller can be copied from:

Update ./config/routes.yaml to look like this:

And update the default template to say Symfony instead of Silex:

Now we should be able to fire up the code with php -S localhost:8080 -t public/ and then see it in a browser. It should show the following message “Welcome to your new Symfony Application!”. Also try an undefined route and then verify that the logs in ./var/log/dev.log are being populated. The finished repo is on GitHub.

The skeleton projects are relatively straight forward and contain both more and less steps then a real project. I’ve converted projects using just two dependencies of symfony/flex and symfony/framework. Also with unit tests and integration tests the conversion is simpler to verify.

If you’re using providers you will need to convert them and if you have a custom namespace then you will need to modify the services.yaml to take that in to account but that’s the basic process of switching a project from Silex to Symfony and preserving the Git history.

--

--

Peter Lafferty

At least 8 months experience at something and a lifetime of loving JCVD movies.