You are browsing the documentation for Symfony 3.4 which is not maintained anymore.
Consider upgrading your projects to Symfony 5.2.
Creating the Project
Creating the Project¶
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.
Use the Symfony Installer to create new Symfony-based projects.
Read the Installing & Setting up the Symfony Framework article learn how to install and use the Symfony Installer.
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:
Linux and macOS systems:
$ cd projects/ $ symfony new blog --version=3.4
> cd projects/ > php symfony new blog --version=3.4
If the installer doesn’t work for you or doesn’t output anything, make sure that the Phar extension is installed and enabled on your computer.
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
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 12 13 14 15 16
blog/ ├─ app/ │ ├─ config/ │ └─ Resources/ ├─ bin │ └─ console ├─ src/ │ └─ AppBundle/ ├─ var/ │ ├─ cache/ │ ├─ logs/ │ └─ sessions/ ├─ tests/ │ └─ 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/config/, stores all the configuration defined for any environment;
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;
var/cache/, stores all the cache files generated by the application;
var/logs/, stores all the log files generated by the application;
var/sessions/, stores all the session files generated by the application;
tests/AppBundle/, stores the automatic tests (e.g. Unit tests) of the application.
vendor/, this is the directory where Composer installs the application’s dependencies and you should never modify any of its contents;
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 applications 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 applications, then it shouldn’t be its own bundle. Moreover, if InvoiceBundle depends on ProductBundle, then there’s no advantage to having two separate bundles.
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.
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.
Another reason to create a new bundle is when you’re overriding something in a vendor’s bundle (e.g. a controller). See How to Use Bundle Inheritance to Override Parts of a Bundle.
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 14 15 16 17 18
blog/ ├─ app/ │ ├─ config/ │ └─ Resources/ ├─ bin/ │ └─ console ├─ src/ │ └─ AppBundle/ ├─ tests/ │ └─ AppBundle/ ├─ var/ │ ├─ cache/ │ ├─ logs/ └─ sessions/ ├─ vendor/ └─ web/ ├─ app.php └─ app_dev.php
If your Symfony installation doesn’t come with a pre-generated AppBundle, you can generate it by hand executing this command:
$ php bin/console generate:bundle --namespace=AppBundle --dir=src --format=annotation --no-interaction
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.