Symfony2 console: tutorial for beginners

Symfony2 console: tutorial for beginners

This tutorial should be useful for those who are interested in Symfony web development. In Symfony, there are many console commands to help you in your work. We will consider the most frequently used Symfony commands. For example, the cache cleaning command:

$ php app/console cache:clear

Almost every console command has optional and mandatory settings or properties. For example, the same cache clearing command may have the following options:

$ php app/console cache:clear --env=dev
$ php app/console cache:clear --env=prod

With the --env=dev and --env=prod options we specify in which environment we are cleaning the cache. In Symfony, there are the following options for each console command:

  • --help (-h) — displays the help message;
  • --quiet (-q) — does not output any message;
  • --verbose (-v|vv|vvv) — increases the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug;
  • --version (-V) — displays the application version;
  • --ansi — forces ANSI output;
  • --no-ansi — disables ANSI output;
  • --shell (-s) — launches the shell;
  • --process-isolation — launches commands from shell as a separate process;
  • --env=[...] (-e=[...]) — sets the environment name (dev or prod, the default is dev);
  • --no-debug — switches off debug mode.

To see the list of all commands, you need to enter the following command: php app/console list or just php app/console.

Symfony console doctrine

The Doctrine2 ORM integration for working with the database offers several console commands under the doctrine namespace. To view the command list you can use the list command.

$ php app/console list doctrine

You can find more detailed information about any of these commands by running the help command. Let’s view some of the most frequently used commands:

  • $ php app/console doctrine:database:create [--connection=...] ⎯ will create a database that is written in your configurations;
  • $ php app/console doctrine:database:drop [--connection=...] [--force] ⎯ will delete the database;
  • $ php app/console doctrine:generate:entity [--entity=...] [--fields=...] [--format=...] [--with-repository] ⎯ will create a new Doctrine object inside your bundle. This command has the following additional options:
  1. --entity ⎯ specifies the name of the entity class;
    --fields ⎯ specifies configurations for new fields;
    --format ⎯ specifies the format for the configuration file (php, xml, yml, or annotation, the default is annotation);
    --with-repository ⎯ generates repository for this entity.

An example of this command:

$ php app/console doctrine:generate:entity --entity=AcmeUserBundle:User 

$ php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Post --format=yml --fields="title:string(255) body:text" --with-repository

$ php app/console doctrine:generate:entities <name>  [--path=...] [--no-backup] ⎯ will create entity classes and their methods according to your configurations. The <name> parameter is mandatory, here we specify the full name of the entity and the bundle. Examples:

$ php app/console doctrine:generate:entities MyCustomBundle
$ php app/console doctrine:generate:entities MyCustomBundle:User
$ php app/console doctrine:generate:entities MyCustomBundle/Entity/User

$ php app/console doctrine:generate:form <entity> ⎯ will generate a form for your <entity>. Example:

$ php app/console doctrine:generate:form  MyCustomBundle:Entity

$ php app/console doctrine:generate:crud [--entity=...] [--route-prefix=...] [--with-write] [--format=...] ⎼ will create CRUD based on Doctrine entity, i.e. generate a controller for this entity with the location in the specified bundle. This controller allows to run five basic operations on the entity:

  1. 1. Outputting the list of all entries;
  2. 2. Displaying one entry by its primary key;
  3. 3. Creating a new entry;
  4. 4. Editing an existing entry;
  5. 5. Deleting an existing entry.


$ php app/console doctrine:generate:crud --entity=AcmeBlogBundle:Post --route-prefix=post --format=annotation

$ php app/console doctrine:schema:create — creates tables according to your configurations;

$ php app/console doctrine:schema:update --force — updates the database scheme;

Symfony console doctrine migration

$ php app/console doctrine:migrations:diff — creates migrations by comparing the current database by entity configurations;

$ php app/console doctrine:migrations:execute <version> [--up] [--down] — the specified migration <version> is added to the database --up or is deleted from the database --down;

$ php app/console doctrine:migrations:generate — creates an empty migration class;

$ php app/console doctrine:migrations:latest — outputs the latest version number;

$ php app/console doctrine:migrations:migrate — executes the migration to a specified version or to the latest version available;

$ php app/console doctrine:migrations:status — shows the list of migrations.

Useful tip: to work with the database, it is better to use migrations instead of schema-tool. When you use schema-tool, the database change history is not saved, and it is a big disadvantage for the production/staging environment. In addition, migrations let your team know when it's time to update their schemas. If you only use schema-tool, your teammates will have to run doctrine:schema:update --force every time, when they pull changes because they will not know if the schema has really changed. When using the migration, you always see that there are some updates in the migration folder, which means that you need to update your schema. If after the migration you realize that you have made some mistakes in the entity or its fields, you can easily cancel the changes by running this command doctrine:migrations:execute <version> [--down]. But migrations in Symfony are not installed by default out of the box, you need to additionally install DoctrineMigrationsBundle by running this command

$ composer require doctrine/doctrine-migrations-bundle "^1.0"

Other Symfony console commands

$ php app/console generate:bundle [--namespace="..."] [--dir="..."] [--bundle-name="..."] [--format="..."] [--structure] — generates a new bundle. By default, the command interacts with the developer to customize the generation. Example:

$ php app/console generate:bundle --namespace=Acme/BlogBundle --dir=src --format=annotation

$ php app/console generate:controller [--controller="..."] [--route-format="..."] [--template-format="..."] [--actions="..."] — the command allows to create a new controller inside the bundle;

--controller="..." — we specify the bundle name in which we create the controller, as well as the name of the controller itself;

--route-format="..." — routing configurations format (yml, xml, php, annotation, the default is annotation);

--actions="..." — creates actions in the controller (multiple values are allowed);

--template-format="..." — the format that is used in creating templates (twig, php, the default is twig).


$ php app/console generate:controller --controller=AcmeBlogBundle:Post

$ php app/console router:debug — displays current routes for the application;

$ php app/console assetic:dump — physically writes all CSS, JavaScript files, pictures necessary for your dev environment. A big drawback is the necessity to run every time web assets are updated. Luckily, with the help of the php app/console assetic:watch command all files will be regenerated automatically if they change;

$ php app/console assets:install — used to install web assets (CSS, JavaScript, pictures). When run without options, the command copies to web/ all files found in the  Resources/public/ directories of your application.

10 votes, Rating: 5

Read also


When the work on your website development is accomplished, there comes this exciting moment. Your new website is ready to start its successful journey into the world...or, better to say, the World...


We have been working hard, and finally our efforts are evaluated: InternetDevels is on a list of 12 Top...


The new Drupal website support service DruDesk by InternetDevels has already gained a lot of happy customers. Yes, people like when all their problems are solved...or, better to say, dissolved in...


Imagine all website issues solved in a blink of an eye - you can just sit in a comfortable chair, have a cup of coffee and enjoy reading nice news. Yes, it’s possible. It’s not magic...or maybe...


In this article we will discuss 2 ways to implement websockets: 1) using Node.js and Tornado web server; 2) using php (Ratchet library) and js.

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime