Structure of a Symfony application (Lesson 3. Basics)

After familiarizing yourself with the Symfony framework. Introduction and successfully installing this framework Installing Symfony 2.*, let's start studying the basics - the Symfony application structure.

The key feature of developing with Symfony is the bundle system. A bundle is a collection of scripts (php, js, etc.) (and not only (html, css, etc.)) that are used to achieve a common goal.

The uniqueness of Symfony is that "everything is a bundle" - your application, even the core functionality. So, to begin with, you need to create a bundle. More details about this will be discussed in the following notes.

The root directory of the application looks as follows:

  • app/ - primarily contains configuration, including routing (About it later too =))
  • bin/
  • src/ - project code, bundles
  • vendor/ - libraries from external developers
  • web/ - what is accessible to end users.
Now a little more detailed. web/ is the directory for all files accessible from the outside. For example, images, css, js files, and others. Also, the application entry script (so-called front controller) is located here. The front controller can be as follows:
// web/app.php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use SymfonyComponentHttpFoundatholds all tests for the bundle.ionRequest;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->handle(Request::createFromGlobals())->send();
In app/, there is the AppKernel, which is responsible for the application configuration.

Usually, during development, you will have to use this directory to change configuration, routing (app/config). This directory also contains cache and logs (app/cache and app/logs), and app/Resources contains application-level templates.

In short, src/ contains all the code (php, css, js, etc.) necessary for the application to work. This is where the bundles (bundle) are located.

 

In turn, a bundle has the following file structure (of course, everything can be changed):

  • Controller/ - bundle controller;
  • DependencyInjection/ ;
  • Resources/config/ - contains configuration, including routing;
  • Resources/views/ - contains templates for display (for example, Hello/index.html.twig);
  • Resources/public/ - contains components required for web application operation, but not code (images, css, etc.). Copied or symlinked to the web/directory using the assets:install command;
  • Tests/ - contains bundle tests.
These basic knowledge are enough to understand the file structure in Symfony.