Bundle system


Bundles are similar to plug-ins in other software, but better. The key difference is this: everything in Symfony is a bundle, including the core functionality of the framework, as well as the program code you write. Bundles are first-class citizens in the Symfony system. This gives you a flexible architecture that allows you to use pre-built functionality from third-party bundles or publish your own bundles. Bundles make it easy to choose which features to enable in your application and optimize them the way you want.

In this article you can learn the basics of bundles, and there is a large chapter in "Best Practices" dedicated to the organization and best practices of bundle.

A bundle is a set of structured files stored in a directory "used to implement an independent function". You can create a BlogBundle, a ForumBundle, or a bundle that manages users (many similar bundles already exist as open source projects). Each directory contains everything related to that functionality, including php files, templates, css, js files, tests, and others. The sub-items of each function exist in the bundle, and each function exists in the bundle.

To use bundles in your program, you must register and use them through the registerBundles() method of the AppKernel class:

// app/AppKernel.phppublic function registerBundles(){
    $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
    );     if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }     return $bundles;

# The ##registerBundles() method gives you complete control over which bundles (including Symfony's core bundles) are used in your program.

bundle can exist anywhere, as long as it can be automatically loaded (through the autoloader configured in

app/autoload.php)

Create a Bundle

The standard version of Symfony has built-in super easy-to-use commands to help you create a full-featured bundle. Of course, creating the bundle manually is just as easy.

In order to demonstrate a simple bundle system, we create a new AcmeTestBundle and open it. The

Acme part is just a pseudonym. In actual practice, it should be replaced by some "vendor" name to represent you or your organization (for example, ABCTestBundle represents a certain A company named ABC)

Start by creating a new

src/Acme/TestBundle/ directory, and then create a new AcmeTestBundle. php File:

// src/Acme/TestBundle/AcmeTestBundle.phpnamespace Acme\TestBundle; 
use Symfony\Component\HttpKernel\Bundle\Bundle; 
class AcmeTestBundle extends Bundle
{
}

AcmeTestBundle follows the bundle naming convention. You can choose to simplify the bundle name to TestBundle by changing the class name to TestBundle (and modify the file name

TestBundle.php)

This empty class is the only thing you need when creating a new bundle. Although it is generally empty, this class is particularly powerful and is specifically used to customize various behaviors of the bundle.

Now that you have created the bundle, enable it in the AppKernel class:

// app/AppKernel.phppublic function registerBundles(){
    $bundles = array(
        // ...         // register your bundle / 注册你的bundle
        new Acme\TestBundle\AcmeTestBundle(),
    );
    // ...     return $bundles;}

Although nothing has been done yet, the AcmeTestBundle is ready to use.

It’s that simple. Symfony also provides a command line interface for generating a basic bundle skeleton:

$  php bin/console generate:bundle --namespace=Acme/TestBundle

The bundle skeleton includes controllers, templates, routing resources, etc., and All can be customized. You can learn more in the later section on Symfony Command Line Tools.

Whether you are creating a new bundle or using a third-party bundle, you should ensure that the bundle is opened by registerBundles(). When using the generate:bundle command, Symfony completes the registration for you.

Bundle Directory Structure

The bundle directory is simple and flexible. By default, the bundle system follows a set of naming conventions to maintain code consistency across all Symfony bundles. Take a look at the AcmeDemoBundle. It includes some of the most common elements of a bundle:

  • Controller/
  • which contains the controller of the bundle (such as `RandomController .php`).
  • DependencyInjection/
  • There are specific Dependency Injection Extension classes used to import service configuration information, register compiler passes, and more (this directory is not required) .
  • Resources/config/
  • Store configuration information, including routing configuration (`routing.yml`, etc.).
  • Resources/views/
  • Storage templates. Organize subfolders by controller name (e.g. `Hello/index.html.twig`).
  • Resources/public/
  • Store web assets resources (pictures, css, etc.), which will be imported into the `web/` directory of the project through hard copy or symlink. The console command `assets:install` is implemented.
  • Tests/
  • Stores all test classes of this bundle.

A bundle may be small or large depending on the function it implements. It contains only the files you need and nothing else.

As you read through the Chinese guide, you will learn how to persist objects to a database, create and validate forms, add translation functionality to your program, write tests, and more. All of these are based on bundles and have their own directories and functions.