How to create a Symfony 2 project

How to create a Symfony 2 project

Damjan Komlenac 19. July 2015 Expert Topics

1 Comment // Reading Time: 4 min.

Symfony is a PHP framework, so it is obvious that you need to have PHP installed on your machine. Let’s say that you already have PHP installed and lets jump to the installation of Symfony. It is recommended to use the Symfony installer.

1. Installing the Symfony installer

Execute these commands:

sudo curl -LsS -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

Now we can use the global ‘symfony’ command in console.

2. Creating a Symfony project

cd Projects
symfony new myProjectName

To see more options, just run the symfony command without arguments. Now you should have a symfony project with folder structure like this:

├─ app/
│  ├─ console
│  ├─ cache/
│  ├─ config/
│  ├─ logs/
│  └─ Resources/
├─ src/
│  └─ AppBundle/
├─ vendor/
└─ web/

All your code should be placed in “AppBundle”. Symfony organises code in bundles. There are two kinds of bundles: Bundles which are part of your application, e.g. AppBundle, and there are bundles which are reusable by others. Those reusable bundles can be installed by composer, and they are installed in the vendor directory.

If you didn’t get AppBundle created automatically, you can create it, and any other, new, bundle by this command:

php app/console generate:bundle

Example of creating a new bundle TypoScriptBundle in folder SGalinski:

php app/console generate:bundle --namespace=SGalinski/TypoScriptBundle --bundle-name=TypoScriptBundle --dir=src --format=annotation --no-interaction

Does your company need a web specialist to talk to on an equal footing?


3. Using composer to install/update your application

Any dependency which you need can simply be added into the “require” section of your composer.json file and run command:

composer update

If you are running composer for the first time, you should run:

composer install

4. Setting up the database

You should already have doctrine installed. You can search for it in vendor directory, or you can look for it in composer.json.

If you have run composer install, you should have been prompted for database parameters. Anyway, check file: "app/config/parameters.yml", it should look something like this:

database_driver: pdo_mysql
database_port: null
database_name: my_db_name
database_user: user_name
database_password: pass
mailer_transport: smtp
mailer_user: null
mailer_password: null
locale: en (or de, it, sr...)
secret: xxx

Adapt the parameters to your needs. To create a database based on your parameters, run this command:

php app/console doctrine:database:create

5. Creating entities

To generate entities through interactive mode in console where you will be asked for entity names, and for names and types of each field, use this command:

php app/console doctrine:generate:entity

After creating entities and if you have defined mapping rules for them, you can generate database tables:

app/console doctrine:schema:update --force

6. Useful console commands

Here is list of commands that are useful for development of Symfony projects.

composer install
installs all dependencies from scratch

composer update
updates dependencies. Use this command after making changes to composer.json

php app/console cache:clear
clears the cache

php app/console doctrine:database:create 
creates empty database based on parameter from "app/config/parameters.yml".

php app/console doctrine:schema:update --dump-sql
shows sql needed to update database model from entities.

php app/console doctrine:schema:update --force 
executes model update based on entities.

php app/console doctrine:migrations:status
shows the status of migrations - which version is current,how many versions is behind and in front of current version.

php app/console doctrine:migrations:migrate
applies all migration versions (by their order) which are in front of current version.

php app/console doctrine:migrations:diff
makes migration version based on current state of entities and current state of database.

php app/console doctrine:migrations:execute --down 20150629191737
Execute a single migration (defined by its number) up or down manually.

php app/console doctrine:generate:entities BundleName:EntityName
generates entities from existing DB model. It can be used even if the DB was generated out of entities for generating getters and setters for the entity.

php app/console doctrine:generate:entity
generates entity through interactive mode in console where user is asked for entity name, and for the name and type of each field.

php app/console assets:install --symlink
installs assets as symbolic links

mysqldump -t -u userName -ppassword dbName tableName
dumps a MySQL database  table for backup. Useful to see SQL statements which represent current database state.

phpunit -c app src/AppBundle/Tests/Entity/RoleRepositoryFunctionalTest.php
runs phpunit tests

Enjoy exploring the world of Symfony! :)

contact us

We are a digital agency specialized in the development of digital products. Our head office is in Munich/Unterföhring - but we work remotely worldwide. Our specialities are websites and portals with TYPO3, e-commerce projects with shopware and iOS and Android apps. In addition, we are also familiar with many other topics in these areas. Contact us in your preferred way: Whether by contact form, e-mail, telephone or in a Whatsapp message.


  • Yassine Guedidi

    Yassine Guedidi

    at 02.08.2015

    In useful console commands list, note that the DoctrineMigrationsBundle is not part of the Symfony Standard Edition. In useful console commands list, note that the DoctrineMigrationsBundle is not part of the Symfony Standard Edition.

    Drop files here
Drop files here