Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Best Practices
  4. Creating the Project
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Installing Symfony
    • Linux and Mac OS X Systems
    • Windows Systems
  • Creating the Blog Application
  • Structuring the Application
    • Application Bundles
  • Extending the Directory Structure

Creating the Project

Edit this page

Warning: You are browsing the documentation for Symfony 2.5, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Creating the Project

Installing Symfony

In the past, Symfony projects were created with Composer, the dependency manager for PHP applications. However, the current recommendation is to use the Symfony Installer, which has to be installed before creating your first project.

Linux and Mac OS X Systems

Open your command console and execute the following:

1
2
3
$ curl -LsS http://symfony.com/installer > symfony.phar
$ sudo mv symfony.phar /usr/local/bin/symfony
$ chmod a+x /usr/local/bin/symfony

Now you can execute the Symfony Installer as a global system command called symfony.

Windows Systems

Open your command console and execute the following:

1
c:\> php -r "readfile('http://symfony.com/installer');" > symfony.phar

Then, move the downloaded symfony.phar file to your projects directory and execute it as follows:

1
c:\> php symfony.phar

Creating the Blog Application

Now that everything is correctly set up, you can create a new project based on Symfony. In your command console, browse to a directory where you have permission to create files and execute the following commands:

1
2
3
4
5
6
7
# Linux, Mac OS X
$ cd projects/
$ symfony new blog

# Windows
c:\> cd projects/
c:\projects\> php symfony.phar new blog

This command creates a new directory called blog that contains a fresh new project based on the most recent stable Symfony version available. In addition, the installer checks if your system meets the technical requirements to execute Symfony applications. If not, you'll see the list of changes needed to meet those requirements.

Tip

Symfony releases are digitally signed for security reasons. If you want to verify the integrity of your Symfony installation, take a look at the public checksums repository and follow these steps to verify the signatures.

Structuring the Application

After creating the application, enter the blog/ directory and you'll see a number of files and directories generated automatically:

1
2
3
4
5
6
7
8
9
10
11
blog/
├─ app/
│  ├─ console
│  ├─ cache/
│  ├─ config/
│  ├─ logs/
│  └─ Resources/
├─ src/
│  └─ AppBundle/
├─ vendor/
└─ web/

This file and directory hierarchy is the convention proposed by Symfony to structure your applications. The recommended purpose of each directory is the following:

  • app/cache/, stores all the cache files generated by the application;
  • app/config/, stores all the configuration defined for any environment;
  • app/logs/, stores all the log files generated by the application;
  • app/Resources/, stores all the templates and the translation files for the application;
  • src/AppBundle/, stores the Symfony specific code (controllers and routes), your domain code (e.g. Doctrine classes) and all your business logic;
  • vendor/, this is the directory where Composer installs the application's dependencies and you should never modify any of its contents;
  • web/, stores all the front controller files and all the web assets, such as stylesheets, JavaScript files and images.

Application Bundles

When Symfony 2.0 was released, most developers naturally adopted the symfony 1.x way of dividing applications into logical modules. That's why many Symfony apps use bundles to divide their code into logical features: UserBundle, ProductBundle, InvoiceBundle, etc.

But a bundle is meant to be something that can be reused as a stand-alone piece of software. If UserBundle cannot be used "as is" in other Symfony apps, then it shouldn't be its own bundle. Moreover InvoiceBundle depends on ProductBundle, then there's no advantage to having two separate bundles.

Best Practice

Create only one bundle called AppBundle for your application logic

Implementing a single AppBundle bundle in your projects will make your code more concise and easier to understand. Starting in Symfony 2.6, the official Symfony documentation uses the AppBundle name.

Note

There is no need to prefix the AppBundle with your own vendor (e.g. AcmeAppBundle), because this application bundle is never going to be shared.

All in all, this is the typical directory structure of a Symfony application that follows these best practices:

1
2
3
4
5
6
7
8
9
10
11
12
13
blog/
├─ app/
│  ├─ console
│  ├─ cache/
│  ├─ config/
│  ├─ logs/
│  └─ Resources/
├─ src/
│  └─ AppBundle/
├─ vendor/
└─ web/
   ├─ app.php
   └─ app_dev.php

Tip

If your Symfony installation doesn't come with a pre-generated AppBundle, you can generate it by hand executing this command:

1
$ php app/console generate:bundle --namespace=AppBundle --dir=src --format=annotation --no-interaction

Extending the Directory Structure

If your project or infrastructure requires some changes to the default directory structure of Symfony, you can override the location of the main directories: cache/, logs/ and web/.

In addition, Symfony3 will use a slightly different directory structure when it's released:

1
2
3
4
5
6
7
8
9
10
11
12
blog-symfony3/
├─ app/
│  ├─ config/
│  └─ Resources/
├─ bin/
│  └─ console
├─ src/
├─ var/
│  ├─ cache/
│  └─ logs/
├─ vendor/
└─ web/

The changes are pretty superficial, but for now, we recommend that you use the Symfony directory structure.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Check Code Performance in Dev, Test, Staging & Production

    Check Code Performance in Dev, Test, Staging & Production

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Fabian Vogler, a Symfony contributor

    Thanks Fabian Vogler (@fabian) for being a Symfony contributor

    2 commits • 15 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch