-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updated the Best Practices for Symfony 4 and Flex #8599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,53 +4,36 @@ 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. | ||
|
||
.. best-practice:: | ||
|
||
Use the Symfony Installer to create new Symfony-based projects. | ||
Use the `Symfony Skeleton`_ and `Composer`_ to create new Symfony-based projects. | ||
|
||
Read the :doc:`/setup` article learn how to install and use the Symfony | ||
Installer. | ||
The **Symfony Skeleton** is a minimal and empty Symfony project which you can | ||
base your new projects on. Unlike past Symfony versions, this skeleton installs | ||
the absolute bare minimum amount of dependencies to make a fully working Symfony | ||
project. Read the :doc:`/setup` article to learn more about installing Symfony. | ||
|
||
.. _linux-and-mac-os-x-systems: | ||
.. _windows-systems: | ||
|
||
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: | ||
In your command console, browse to a directory where you have permission to | ||
create files and execute the following commands: | ||
|
||
.. code-block:: terminal | ||
|
||
$ cd projects/ | ||
$ symfony new blog | ||
|
||
# Windows | ||
c:\> cd projects/ | ||
c:\projects\> php symfony new blog | ||
|
||
.. note:: | ||
|
||
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. | ||
$ composer create-project symfony/skeleton 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. | ||
project based on the most recent stable Symfony version available. | ||
|
||
.. 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. | ||
The technical requirements to run Symfony are simple. If you want to check | ||
if your system meets those requirements, read :doc:`/reference/requirements`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about recommending using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree! The linked article (https://symfony.com/doc/master/reference/requirements.html) was revamped recently to remove everything and just explain how to use req-checker. |
||
|
||
Structuring the Application | ||
--------------------------- | ||
|
@@ -61,47 +44,29 @@ number of files and directories generated automatically: | |
.. code-block:: text | ||
|
||
blog/ | ||
├─ app/ | ||
│ ├─ config/ | ||
│ └─ Resources/ | ||
├─ bin | ||
├─ bin/ | ||
│ └─ console | ||
├─ config/ | ||
└─ public/ | ||
│ └─ index.php | ||
├─ src/ | ||
│ └─ AppBundle/ | ||
│ └─ Kernel.php | ||
├─ var/ | ||
│ ├─ cache/ | ||
│ ├─ logs/ | ||
│ └─ sessions/ | ||
├─ tests/ | ||
│ └─ AppBundle/ | ||
├─ vendor/ | ||
└─ web/ | ||
│ └─ log/ | ||
└─ vendor/ | ||
|
||
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/log/``, 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; | ||
* ``web/``, stores all the front controller files and all the web assets, such | ||
as stylesheets, JavaScript files and images. | ||
structure your applications. It's recommended to keep this structure because it's | ||
easy to navigate and most directory names are self-explanatory, but you can | ||
:doc:`override the location of any Symfony directory </configuration/override_dir_structure>`: | ||
|
||
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, | ||
apps used 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 | ||
|
@@ -111,64 +76,11 @@ 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. | ||
|
||
.. 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. | ||
|
||
.. note:: | ||
|
||
Another reason to create a new bundle is when you're overriding something | ||
in a vendor's bundle (e.g. a controller). See :doc:`/bundles/inheritance`. | ||
|
||
All in all, this is the typical directory structure of a Symfony application | ||
that follows these best practices: | ||
|
||
.. code-block:: text | ||
|
||
blog/ | ||
├─ app/ | ||
│ ├─ config/ | ||
│ └─ Resources/ | ||
├─ bin/ | ||
│ └─ console | ||
├─ src/ | ||
│ └─ AppBundle/ | ||
├─ tests/ | ||
│ └─ AppBundle/ | ||
├─ var/ | ||
│ ├─ cache/ | ||
│ ├─ logs/ | ||
└─ sessions/ | ||
├─ 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: | ||
|
||
.. code-block:: terminal | ||
|
||
$ php bin/console generate:bundle --namespace=AppBundle --dir=src --format=annotation --no-interaction | ||
|
||
Extending the Directory Structure | ||
--------------------------------- | ||
Don't create any bundle to organize your application logic. | ||
|
||
If your project or infrastructure requires some changes to the default directory | ||
structure of Symfony, you can | ||
:doc:`override the location of the main directories </configuration/override_dir_structure>`: | ||
``cache/``, ``logs/`` and ``web/``. | ||
Symfony applications can still use third-party bundles (installed in ``vendor/``) | ||
to add features, but you should use PHP namespaces instead of bundles to organize | ||
your own code. | ||
|
||
.. _`Symfony Skeleton`: https://github.com/symfony/skeleton | ||
.. _`Composer`: https://getcomposer.org/ | ||
.. _`Phar extension`: http://php.net/manual/en/intro.phar.php | ||
.. _`public checksums repository`: https://github.com/sensiolabs/checksums | ||
.. _`these steps`: http://fabien.potencier.org/signing-project-releases.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this paragraph.