Symfony2 User List example

I am going to run through setting up a User list in Symfony2 to help you get started using this framework.

Look through to get an understanding on how to setup this framework on your local environment.

My working bundle is available at: should you wish to see the full working code

Create a Bundle

In the command line go to your downloaded Symfony2 code

php app/console generate:bundle
Bundle name [UsersListBundle]: (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!

Go to your browser with the url


You should get

Hello glyn!

which shows the bundle is setup correctly

Setup a new URL

We want to setup a new URL to list all users

Copy the file:
and name it:

Edit this controller to:


namespace Users\Bundle\ListBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class UsersController extends Controller
     * @Route("/users")
     * @Template()
    public function indexAction($name="")
        return array('name' => $name);

Notice I changed the Class name and the @Route

Copy the folder
Name it:

Now go to your browser to the URL
You should see

“Hello !”

So we now have a new url to list out our users

Create database

Starting with this original file doctrine will create the database table for you and add functions for you to use
Location: src/Users/Bundle/ListBundle/Entity/Users.php

In the command line type:

php app/console doctrine:schema:create

php app/console doctrine:schema:update –force

Now you will see in your database this table created

php app/console doctrine:generate:entities Users/Bundle/ListBundle/Entity/Users

This command makes sure that all of the getters and setters are generated for the Users class. This is a safe command – you can run it over and over again: it only generates getters and setters that don’t exist (i.e. it doesn’t replace your existing methods).

Making Database Calls

All these calls for my example are in /symfonyexample/src/Users/Bundle/ListBundle/Controller/DefaultController.php

List all records

So my using Doctrine you call which repository you would like to retrieve information from followed by the findAll() call to get all records

using the TWIG templating engine it can them loop out all records passed with the following code:

Add/Edit record

So the form is created using $this->createFormBuilder($user) to validation is added automatically and the html for the form is in $form->createView()

The twig template displays this information to the user

The saving of this information is also managed by Doctrine using

$em = $this->getDoctrine()->getManager();

Delete a record

So this code finds the users record to ensure it existsThen uses Doctrine to delete the record from the database

$em = $this->getDoctrine()->getManager();

Then redirects the user back to the list page so any refreshing of the pages will not try to delete the same record again.

Redirect to a page

return $this->redirect($this->generateUrl(‘users_list_users_index’), 301);

If you do not add the 301 redirect part then the url will not change and just show the content of the desired page
I prefer to change the url incase the user bookmarks the page or refreshes it.


If you don’t know which route to direct to, the following command line will help

php app/console router:debug

Shows all the routes setup


This was a simple update.
Edit /symfonyexample/src/Users/Bundle/ListBundle/Controller/DefaultController.php

Change @Route(“/”) so it runs from just / in the url
Edit the template /symfonyexample/src/Users/Bundle/ListBundle/Resources/views/Default/index.html.twig
So you provide a link through to the users page

Setup assets

My css file are in:

In the console run:

php app/console assets:install web

Now the CSS include will work for the path:


Now clear the cache in the console as shown in the helplist below and your user list website is good to go.

I have not covered the unit testing and changing the error templates in this post so it has been left out of my example code.

Console Helplist

To use without app_dev.php in the url:
php app/console cache:clear --env=prod --no-debug
Shows all the routes setup:
php app/console router:debug
Setup assets
php app/console assets:install web
Setup database
php app/console doctrine:schema:update --force
Setup Entities
php app/console doctrine:generate:entity
Create new bundle
php app/console generate:bundle
Generate Getters and Setters
php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product

Leave a Reply

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