Symfony: 2 – Directory structure

Autoloading

When Symfony is loading, a special file – vendor/autoload.php – is included. This file is created by Composer and will autoload all application files living in the src/ folder as well as all third-party libraries mentioned in the composer.json file.

Because of the autoloader, you never need to worry about using include or require statements. Instead, Composer uses the namespace of a class to determine its location and automatically
includes the file on your behalf the instant you need a class. The autoloader is already configured to look in the src/ directory for any of your PHP classes. For autoloading to work, the class name and path to the file have to follow the same pattern:

Class Name:
  Test\HelloBundle\Controller\HelloController
Path:
  src/Test/HelloBundle/Controller/HelloController.php

The directory structure

Each Symfony application has the same basic and recommended directory structure:

  • app/: This directory contains the application configuration
  • src/: All the project PHP code is stored under this directory
  • vendor/: Any vendor libraries are placed here by convention
  • web/: This is the web root directory and contains any publicly accessible files

The Application (app) Directory
The AppKernel class is the main entry point of the application and is responsible for all configuration. As such, it is stored in the app/ directory. This class must implement two methods that define everything that Symfony needs to know about your application. You don’t even need to worry about these methods when starting – Symfony fills them in for you with sensible defaults.

  • registerBundles(): Returns an array of all bundles needed to run the application
  • registerContainerConfiguration(): Loads the main application configuration resource file

In day-to-day development, you’ll mostly use the app/ directory to modify configuration and routing files in the app/config/ directory. It also contains the application cache directory (app/cache), a log directory (app/logs) and a directory for application-level resource files, such as templates (app/Resources).

The Source (src) Directory
The src/ directory contains all of the actual code (PHP code, templates, configuration files, stylesheets, etc) that drives your application. When developing, the vast majority of your work will be done inside one or more bundles that you create in this directory.

The Web Directory
The web root directory is the home of all public and static files including images, stylesheets, and JavaScript files. It is also where each front controller lives:

// web/app.php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
 
use Symfony\Component\HttpFoundation\Request;
 
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->handle(Request::createFromGlobals())->send();

The front controller file, app.php is the actual PHP file that’s executed when using a Symfony2 application and its job is to use a Kernel class, AppKernel, to bootstrap the application.

Bundle Directory Structure

Everything is a bundle in Symfony, including both the core framework functionality and the code written for your application. A bundle is simply a structured set of files within a directory that implement a single feature.

The directory structure of a bundle is simple and flexible. By default, the bundle system follows a set of conventions that help to keep code consistent between all Symfony2 bundles:

  • Controller/ contains the controllers of the bundle (e.g. HelloController.php)
  • DependencyInjection/ holds certain dependency injection extension classes, which may import service configuration, register compiler passes or more (this directory is not necessary)
  • Resources/config/ houses configuration, including routing configuration (e.g. routing.yml)
  • Resources/views/ holds templates organized by controller name (e.g. Hello/index.html.twig)
  • Resources/public/ contains web assets (images, stylesheets, etc) and is copied or symbolically linked into the project web/ directory via the assets:install console command
  • Tests/ holds all tests for the bundle

Leave a Reply