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:
- --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. Outputting the list of all entries;
- 2. Displaying one entry by its primary key;
- 3. Creating a new entry;
- 4. Editing an existing entry;
- 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]
$ 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]
$ 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;