A bundle is similar to a plug-in other frameworks, but it performs better than a plug-in. The main difference between it and other frameworks is that everything in symfony2 is bundle, including core framework functions and all application code you write. In symfony2, bundle is a first-class citizen. This gives you more flexibility to use content packages developed by other third parties or to distribute your own bundle. You can easily select the content that can be applied to your program and optimize it based on your ideas.
A bundle is a directory with good structure. It can store everything from class to controller and web resources.
A bundle is just a set of structured file directories that implement a single content.
You can create a blogbundle, A forumbundle, or a bundle that implements user management (as if there are already many such open source bundle ). Each bundle directory contains everything related to the implementation content, including the PHP file, template, style sheet, Javascript file, test content, and anything else. All aspects of the content to be implemented are stored in a bundle.
An application is composed of all bundle defined in the registerbundles () method in 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 JMS\SecurityExtraBundle\JMSSecurityExtraBundle(), ); if (in_array($this->getEnvironment(), array(‘dev‘, ‘test‘))) { $bundles[] = new Acme\DemoBundle\AcmeDemoBundle(); $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); } return $bundles;}
Here you can use this method to centrally control and manage your application composition.
A bundle can be stored in any directory. It can be automatically loaded by configuring the auto loader in the app/autoload. php file.
Create a bundle
In the symfony2 Standard Edition, you have prepared a full-Function Tool file for creating bundle. You can run it to create all the contents of the bundle. Of course, you can also
Select create by yourself. Now we create an acmetestbundle and enable it to work in our applications. Note that the Acme here is a false provider name. You can replace it with your own organization or company name.
First, create a src/Acme/testbundle/directory and add the new file acmetestbundle. php.
// src/Acme/TestBundle/AcmeTestBundle.phpnamespace Acme\TestBundle;use Symfony\Component\HttpKernel\Bundle\Bundle;class AcmeTestBundle extends Bundle{}
Next, to make it available in your application, you need to add it to the registerbundles () method in the appkernel class.
// app/AppKernel.phppublic function registerBundles(){ $bundles = array( // ... // register your bundles new Acme\TestBundle\AcmeTestBundle(), ); // ... return $bundles;}
Although it cannot do anything now, it has become part of your application.
We can also use symfony2 to create a command line tool for us:
$ php app/console generate:bundle --namespace=Acme/TestBundle
If you use the preceding command line tool, the created bundle is automatically registered to the appkernel class.
Bundle directory structure
Let's take a look at the directory structure of the demo bundle that comes with symfony2:
Bundle's directory structure is simple and flexible, as shown in the preceding figure:
Controller/All controllers files containing bundle, such as hellocontroller. php.
Dependencyinjection/saves a specific dependency injection extension class, which may import service configurations, register the compiler for transmission, or more. This directory is not required.
Resources/config/stores the configuration file, including route configuration (for example, routing. yml ).
Resources/views/all templates are divided into folders according to the name of the corresponding controller and saved here. For example, hello/index.html. Twig.
Resources/public/all accessible web resources (images, style sheets, etc.) and content that is asynchronously linked to the project web/directory by running the assets: Install console command.
Tests/save all the tests of bundle
Below are some standard bundle rules recommended by symfony2:
Bundle Name:
A bundle is also a PHP namespace. The namespace must comply with the internal technical standards of the php5.3 namespace and class name. Start with the provider name, followed by the category segment (which can be omitted), and finally the abbreviated namespace name. The name must be suffixed with bundle. To change a namespace to a bundle, you only need to add a bundle class to the namespace.
Bundle Class Name:
Only numbers, letters, and underscores are allowed.
Use the camper name
Use a descriptive and concise name (no more than two words)
Prefix with vendor name (optional classification namespace)
Add bundle as name suffix
For example:
Namespace => bundle Class Name
Acme\Bundle\BlogBundle => AcmeBlogBundleAcme\Bundle\Social\BlogBundle =>AcmeSocialBlogBundleAcme\BlogBundle => AcmeBlogBundle
The getname () method when defining the bundle class should return the class name.
Each bundle has an alias. It is the bundle name in the simplified lowercase character version and is separated by underscores. For example, the bundle of acme_hello is named "acmehellobundle", and "acme_social_blog" is an instance of "Acme \ social \ blogbundle.
Aliases must be unique in a bundle.
Bundle directory structure: the Basic directory structure of hellobundle
XXX/... HelloBundle/ HelloBundle.php Controller/ Resources/ meta/ LICENSE config/ doc/ index.rst translations/ views/ public/ Tests/
The above xxx/... maps to the namespace of the bundle. The following files are required:
Hellobundle. php;
Resources/META/license: Full-text license code;
Resources/doc/index. rst: Specifies the root directory file of bundle.
The depth of subfolders of classes should be kept to a minimum (level 2 is the limit ). More levels can be defined as non-static, but rarely used. The bundle directory is read-only. If you need to modify temporary files, save them to the cache/or log/directory of the main application.
Classes and files to be emphasized
Type vs directory
Commands vs command/
Controllers vs Controller/
Service container extensions VS/dependencyinjection/
Event Listeners vs eventlistener/
Configuration vs resources/config
Web resources vs resources/Public
Translation files vs resources/translations/
Templates vs resources/Views
Unit and functional test vs tests/
Class:
The bundle directory structure is used as the namespace level. For example, the hellocontroller class is saved in the bundle/hellobundle/controller/hellocontroller. php file.
Therefore, the full name of the class is bundle \ hellobundle \ controller \ hellocontroller. Some classes are regarded as decoration, and the shorter the better, such as commands, helpers, listeners, and controllers, are generally treated as suffixes.
Classes related to the event distributor should be identified with the suffix listener.
The exception class should be saved to an exception sub-namespace.
About providers
A bundle should not be embedded in a third-party PHP class library. It should be automatically loaded Based on the symfony2 standard.
A bundle should not be embedded in a third-party JavaScript, CSS, or any class library written in other languages.
Test
A bundle should have a test unit using phpunit and store it in the tests/directory.
The test should follow the following principles:
The test suite must be able to be executed by a simple phpunit command from a simple application.
Function Testing should only be used to test the response output and some monitoring information.
The test code should cover at least 95% of the basic code.
A test suite may not contain the alltests. php script, but it must rely on external phpunit. xml. Dist files.
Document Description
All classes must contain phpdoc.
Controllers
In the best case, the Controller should be in a bundle that can be deployed elsewhere, so it cannot inherit the controller base class. Instead, you can inherit the controller by implementing the containerawareinterface interface or inheriting containeraware.
Routing
If bundle provides routes, they must use the bundle alias as the prefix. For example, if an acmeblogbundle instance is used, all Route names must start with acme_blog.
Templates
If bundle provides a template, it must use twig. Bundle does not need to be downgraded to a master layout file, unless your bundle is a complete application.
Translate files
If a bundle provides information translation, it must be defined in xliff format, and the region name must be named after the bundle name, such as bundle. Hello.
Configuration
To provide greater flexibility, a bundle can use the built-in mechanism of symfony2 to provide configuration settings. Simple settings depend on the default symfony2 Parameters Configuration entry. The symfony2 parameter is a simple key/value pair. The value can be any valid PHP value. Each parameter name should start with the alias of bundle. This is only the best recommendation. The remaining parts of the parameter name are separated by the dot (.), for example, acme_hello.email.from
This allows end users to directly provide value information in the configuration file.
Yaml format:
# app/config/config.ymlparameters: acme_hello.email.from: [email protected]
XML format:
<!-- app/config/config.xml --><parameters> <parameter key="acme_hello.email.from">[email protected]</parameter></parameters>
PHP code format:
// app/config/config.php$container->setParameter(‘acme_hello.email.from‘, ‘[email protected]‘);
INI format:
[parameters]acme_hello.email.from = [email protected]
In this way, you can get the configuration information from the container in the Code:
$container->getParameter(‘acme_hello.email.from‘);
If you define a service, we recommend that you use the bundle alias as the prefix.
Summary:
The above is the general situation of bundle, the most important plug-in format in symfony2. Almost all of the applications developed based on symfony2 are composed of bundle. The core components of symfony2 are frameworkbundle. In the symfony2 communication community, a large number of developers have contributed to their bundle. We can directly integrate them into our own applications. Most of the rules mentioned above are applicable to the unified rules you should follow when developing the contribution bundle to facilitate other users.
Symfony2 development kit with third-party contribution:
If you are not planning to contribute your bundle, you do not need to develop it according to most of the rules described here.