The Bundle System

A bundle is similar to a plugin in other software, but even better. The key
difference is that everything is a bundle in Symfony, including both the
core framework functionality and the code written for your application.
Bundles are first-class citizens in Symfony. This gives you the flexibility
to use pre-built features packaged in third-party bundles or to distribute
your own bundles. It makes it easy to pick and choose which features to enable
in your application and to optimize them the way you want.

Note

While you'll learn the basics here, an entire article is devoted to the
organization and best practices of bundles.

A bundle is simply a structured set of files within a directory that implement
a single feature. You might create a BlogBundle, a ForumBundle or
a bundle for user management (many of these exist already as open source
bundles). Each directory contains everything related to that feature, including
PHP files, templates, stylesheets, JavaScript files, tests and anything else.
Every aspect of a feature exists in a bundle and every feature lives in a
bundle.

Bundles used in your applications must be enabled by registering them in
the registerBundles() method of the AppKernel class:

The name AcmeTestBundle follows the standard
Bundle naming conventions. You could
also choose to shorten the name of the bundle to simply TestBundle by naming
this class TestBundle (and naming the file TestBundle.php).

This empty class is the only piece you need to create the new bundle. Though
commonly empty, this class is powerful and can be used to customize the behavior
of the bundle.

Now that you've created the bundle, enable it via the AppKernel class:

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 Symfony bundles. Take a look at AcmeDemoBundle, as it contains some
of the most common elements of a bundle:

Controller/

Contains the controllers of the bundle (e.g. RandomController.php).

DependencyInjection/

Holds certain Dependency Injection Extension classes, which may import service
configuration, register compiler passes or more (this directory is not
necessary).

A bundle can be as small or large as the feature it implements. It contains
only the files you need and nothing else.

As you move through the guides, you'll learn how to persist objects to a
database, create and validate forms, create translations for your application,
write tests and much more. Each of these has their own place and role within
the bundle.