Symfony2 Starters Guide

I am going to run through the quick setup of a website using Symfony2.

Get the Basic setup

I am skipping the Composer method of installation for those who are more comforatble with command line programming
Download the latest stable version of Symfony
At the time of this post it is Symfony Standard 2.3.6

Extract to your local hosting environment
In my case I am using wamp so:

You can now see the following folder structure
The web root of your document is in the /web folder

If you open your new website in a browser through your usual method (http://localhost/ or custom local domain you set up)

Oops! An Error Occurred
The server returned a “404 Not Found”.

Don’t worry, this is fine, just means that you havn’t completed the setup just yet.

In your brower go to /config.php (so http://localhost/config.php or similar)

Change any of the PHP settings they recommend if you need to
(You can ignore the PHP accelerator on your development environment)

Click on

Configure your Symfony Application online

Database settings
Create your database and users in your normal way
Enter them into the browser to setup your website
This saves your settings in (app/config/parameters.yml)

Next Step

Global secret page
Generate a secret from the onscreen button

Next step

Hopefully you now get the message:

Your distribution is configured!


Go to Welcome page

Add New Pages

Lets create our own page
You will want to create a new bundle so your code is nice and modular
You could copy and paste the existing DemoBundle if you wanted to but it takes a bit of time to set everything up so it would be better to create a new bundle from the command line

Open command prompt (or your terminal if you are on MAC or linux)
cd C:\wamp\www\github\symfonyexample
php app/console generate:bundle

If you don’t have PHP available in the command line look at
You should now see

Bundle namespace: Example/Bundle/exampleBundle
Bundle name [ExampleexampleBundle]: (Press enter)
Target directory [C:/wamp/www/github/symfonyexample/src]: (Press enter)
Configuration format (yml, xml, php, or annotation): annotation
Do you want to generate the whole directory structure [no]? yes
Do you confirm generation [yes]? yes
Confirm automatic update of your Kernel [yes]? yes
Confirm automatic update of your Routing [yes]? yes
You can now start using the generated code!

I selected annotation so all the routing elements remain inside the main bundle so it is easy to remove blocks in future

View development page

So in your browser go to:
and you will see

Hello glyn!

which is added in automatically in your example bundle
If you try http://localhost/hello/glyn you might get an error page like before

What is /app_dev.php

Symfony2 comes with a built in debug mode, which is what you are using when you access url’s with the app_dev.php addition which caches less than the production mode.
On the command line run
php app/console cache:clear --env=prod --no-debug
You should now be able to view http://localhost/hello/glyn successfully

View the code


You can change @Route to any location not already created (i.e. @Route(“/goodbye/{name}”))

To set a default value for {name} you can change @Route to:
@Route(“/hello/{name}”, requirements={“name” = “\w+”}, defaults={“name” = “Fred”})
so the url /hello will return “Hello Fred!”
Make sure you remain on http://localhost/app_dev.php/ when testing or you will need to clear your cache again manually


Delete Bundles

Lets remove this bundle just so you know how to do this
Delete folder: C:\wamp\www\github\symfonyexample\src\Example

Edit file: C:\wamp\www\github\symfonyexample\app\AppKernel.php
Remove line:

new Example\Bundle\exampleBundle\ExampleexampleBundle(),

Edit file: C:\wamp\www\github\symfonyexample\app\config
Remove the lines:

resource: “@ExampleexampleBundle/Controller/”
type:     annotation
prefix:   /

Now this bundle has been completly deleted


Symfony comes with its own text suite so you can check that any updates you make to your code do not break the site elsewhere
You can see these in C:\wamp\www\github\symfonyexample\src\Example\Bundle\exampleBundle\Tests\Controller
This is all command line driven so view for more information


I will cover a full website build in my next post providing more information on how to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *