Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. The Bundle System
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Creating a Bundle
  • Bundle Directory Structure
  • Learn more

The Bundle System

Edit this page

The Bundle System

Caution

In Symfony versions prior to 4.0, it was recommended to organize your own application code using bundles. This is no longer recommended and bundles should only be used to share code and features between multiple applications.

A bundle is similar to a plugin in other software, but even better. The core features of Symfony framework are implemented with bundles (FrameworkBundle, SecurityBundle, DebugBundle, etc.) They are also used to add new features in your application via third-party bundles.

Bundles used in your applications must be enabled per environment in the config/bundles.php file:

1
2
3
4
5
6
7
8
9
10
11
12
// config/bundles.php
return [
    // 'all' means that the bundle is enabled for any Symfony environment
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    // this bundle is enabled only in 'dev' and 'test', so you can't use it in 'prod'
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
];

Tip

In a default Symfony application that uses Symfony Flex, bundles are enabled/disabled automatically for you when installing/removing them, so you don't need to look at or edit this bundles.php file.

Creating a Bundle

This section creates and enables a new bundle to show there are only a few steps required. The new bundle is called AcmeTestBundle, where the Acme portion is an example name that should be replaced by some "vendor" name that represents you or your organization (e.g. AbcTestBundle for some company named Abc).

Start by adding creating a new class called AcmeTestBundle:

1
2
3
4
5
6
7
8
// src/AcmeTestBundle.php
namespace Acme\TestBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class AcmeTestBundle extends AbstractBundle
{
}

6.1

The AbstractBundle was introduced in Symfony 6.1.

Caution

If your bundle must be compatible with previous Symfony versions you have to extend from the Bundle instead.

Tip

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:

1
2
3
4
5
// config/bundles.php
return [
    // ...
    Acme\TestBundle\AcmeTestBundle::class => ['all' => true],
];

And while it doesn't do anything yet, AcmeTestBundle is now ready to be used.

Bundle Directory Structure

The directory structure of a bundle is meant to help to keep code consistent between all Symfony bundles. It follows a set of conventions, but is flexible to be adjusted if needed:

src/
Contains all PHP classes related to the bundle logic (e.g. Controller/RandomController.php).
config/
Houses configuration, including routing configuration (e.g. routing.yaml).
templates/
Holds templates organized by controller name (e.g. random/index.html.twig).
translations/
Holds translations organized by domain and locale (e.g. AcmeTestBundle.en.xlf).
public/
Contains web assets (images, stylesheets, etc) and is copied or symbolically linked into the project public/ directory via the assets:install console command.
assets/
Contains JavaScript, CSS, images and other assets related to the bundle that are not in public/ (e.g. stimulus controllers)
tests/
Holds all tests for the bundle.

Caution

The recommended bundle structure was changed in Symfony 5, read the Symfony 4.4 bundle documentation for information about the old structure.

When using the new AbstractBundle class, the bundle defaults to the new structure. Override the Bundle::getPath() method to change to the old structure:

1
2
3
4
5
6
7
class AcmeTestBundle extends AbstractBundle
{
    public function getPath(): string
    {
        return __DIR__;
    }
}

Tip

It's recommended to use the PSR-4 autoload standard: use the namespace as key, and the location of the bundle's main class (relative to composer.json) as value. As the main class is located in the src/ directory of the bundle:

1
2
3
4
5
6
7
8
9
10
11
12
{
    "autoload": {
        "psr-4": {
            "Acme\\TestBundle\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Acme\\TestBundle\\Tests\\": "tests/"
        }
    }
}

Learn more

  • How to Override any Part of a Bundle
  • Best Practices for Reusable Bundles
  • How to Create Friendly Configuration for a Bundle
  • How to Load Service Configuration inside a Bundle
  • How to Simplify Configuration of Multiple Bundles
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 6.2 is backed by

    Symfony 6.2 is backed by

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Check Code Performance in Dev, Test, Staging & Production

    Check Code Performance in Dev, Test, Staging & Production

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Thomas Ploch, a Symfony contributor

    Thanks Thomas Ploch for being a Symfony contributor

    1 commit • 35 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia