Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of plug-in format in Symfony2

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of a sample analysis of the format of plug-ins in Symfony2. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows:

A bundle is similar to plug-ins in other frameworks, but performs better than plug-ins. The main difference from other frameworks is that everything in Symfony2 is bundle, including core framework functions and all the 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 choose what can be applied to your program and what you don't need to optimize it according to your ideas.

A bundle is a directory that is well structured and can store everything from classes to controller and web resources.

A bundle is simply a structured collection of file directories that implements a single content.

You can create a BlogBundle, a ForumBundle, or a bundle that implements user management (there seem to be many such open source bundle already). Each bundle directory contains everything related to the implementation, including PHP files, templates, stylesheets, javascript files, test content, and anything else. All aspects of the content to be implemented are stored in a bundle.

An application consists of all the 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\ new JMS\ new JMS () ) 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 control and manage the composition of your application.

A bundle can be stored in any directory and can be loaded automatically by configuring the autoloader in the app/autoload.php file.

Create a bundle

A full-featured tool file for creating bundle is ready for you in Symfony2 Standard Edition. You can run it to create all the content of bundle, and of course you can

Choose to create it by hand. Now let's create an AcmeTestBundle and make it work in our application. Note that the Acme here is a fake provider name, and you can replace it with the name of your own organization or company.

First, create a src/Acme/TestBundle/ directory and add a 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 can't do anything now, it has become part of your application.

We can also use Symfony2 to provide us with command-line tools to create:

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

If you use the command line tool above, the created bundle will be automatically registered with the appKernel class.

Directory structure of Bundle

Take a look at the directory structure of the Demo bundle that comes with Symfony2:

The directory structure of bundle is simple and flexible, as can be seen from the screenshot above:

Controller/ contains all the controllers files for bundle, such as HelloController.php.

DependencyInjection/ holds specific dependency injection extension classes that may import service configurations, register compiler transfers, or more. This directory is not required.

Resources/config/ holds configuration files, including routing configurations (such as routing.yml).

All templates of Resources/views/ are stored here in folders according to the name of the corresponding controller. Like Hello/index.html.twig.

Resources/public/ all accessible web resources (pictures, stylesheets, etc.) and contents that are copied or asynchronously linked to the project web/ directory through assets:install console commands.

Tests/ saves all bundle tests

Here are some standard rules for bundle recommended by Symfony2:

Bundle name:

A bundle is also a namespace for PHP. Namespaces must follow the internal technical standards for PHP5.3 namespaces and class names. Start with the provider name, followed by the classification section (which can be omitted), and finally the abbreviated name of the namespace, which must be suffixed with Bundle. Turning a namespace into a bundle only requires you to add a bundle class to that namespace.

Naming of the Bundle class:

Numbers, letters and underscores only

Use hump naming

Use descriptive and concise names (no more than two words)

Prefix with vendor name (optional classification namespace)

Add Bundle as the name suffix

For example:

Namespace = > Bundle class name

Acme\ Bundle\ BlogBundle = > AcmeBlogBundleAcme\ Bundle\ Social\ BlogBundle = > AcmeSocialBlogBundleAcme\ BlogBundle = > AcmeBlogBundle

The getName () method when defining a bundle class should return the class name.

Each bundle has an alias, which is an abbreviated version of the bundle name for lowercase characters, separated by underscores. For example, the bundle name of acme_hello is AcmeHelloBundle, and acme_social_blog is an instance of Acme\ Social\ BlogBundle.

Aliases must be unique in a bundle.

Directory structure of Bundle: basic directory structure of HelloBundle

XXX/... HelloBundle/ HelloBundle.php Controller/ Resources/ meta/ LICENSE config/ doc/ index.rst translations/ views/ public/ Tests/

The XXX/... above The namespace mapped to the bundle. The following files are required:

HelloBundle.php

Resources/meta/LICENSE: license code for full text

Resources/doc/index.rst: the root directory file of the bundle description.

The depth of subfolders that use classes should be kept to a minimum (level 2 is the limit). If more multilevel can be defined as non-static, but rarely used. The directory of bundle 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 emphasize

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 directory structure of bundle is used as a namespace level. For example, the HelloController class is saved in the Bundle/HelloBundle/Controller/HelloController.php file.

So the fully qualified name of the class is Bundle\ HelloBundle\ Controller\ HelloController. Some classes are seen as decorations and should be as short as possible, such as Commands,Helpers, Listeners, and Controllers, which are generally used as suffixes.

Classes related to event dispatchers should be identified by the suffix Listener.

Exception classes should be saved to an exception subnamespace.

About the provider

An bundle should not be embedded in a third-party PHP class library, it should rely on the Symfony2 standard to load them automatically.

A bundle should not be embedded in a third-party javascript,CSS or any class library written in other languages.

About testing

A bundle should have a test unit that uses PHPUnit and store it in the Tests/ directory.

Testing should follow the following principles:

The test suite must be executable from a simple application by a simple phpunit command.

Functional tests should only be standby to test response output and some monitoring information.

The test code should cover at least 95% of the basic code.

A test suite may not contain AllTests.php scripts, but must rely on external phpunit.xml.dist files.

Documentation description

All classes must have PHPDoc.

Controllers

At best, controller should be in a bundle that can be deployed elsewhere, so it cannot inherit the Controller base class. Instead, inherit Controller is replaced by implementing the ContainerAwareInterface interface or inheriting ContainerAware.

Routing

If bundle provides routing, they must use the alias of bundle as a prefix, such as an AcmeBlogBundle instance, and all routing names must begin with acme_blog_.

Templates

If bundle provides templates, it must use Twig. Bundle does not have to lowpass a master layout file, except if your bundle is a complete application.

Translation document

If 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 more flexibility, a bundle can use Symfony2's built-in mechanism to provide configuration settings. For simple settings, it depends on the default parameters configuration entry for Symfony2. The Symfony2 parameters are simple key/value pairs. The value can be any legal PHP value. It is only a best suggestion that each parameter name should start with an alias of error bundle. The rest of the parameter name is marked with a period (.) Split, such as acme_hello.email.from

Allows the end user to provide value information directly in the configuration file.

YAML format:

# app/config/config.ymlparameters: acme_hello.email.from: fabien@example.com

XML format:

Fabien@example.com

PHP code format:

/ / app/config/config.php$container- > setParameter ('acme_hello.email.from',' fabien@example.com')

INI format:

[parameters] acme_hello.email.from = fabien@example.com

This allows you to get this configuration information from the container in the code:

$container- > getParameter ('acme_hello.email.from')

If you define a service, we also recommend that you use the alias of bundle as a prefix.

Sum up and think:

The above is a general picture of the main plug-in format bundle in Symfony2. Almost all applications developed on the basis of Symfony2 are made up of bundle. The core component of Symfony2 itself is FrameworkBundle. In the Symfony2 communication community, there are already a large number of developers who have contributed their bundle, which we can integrate directly into our own applications. Most of the above rules are applied to the uniform rules that you should follow when developing and contributing bundle to facilitate other users to use them.

Symfony2 development package with bundle contributed by a third party:

If you're not going to contribute your bundle, you don't have to follow most of the rules here.

Thank you for reading! This is the end of this article on "sample analysis of plug-in format in Symfony2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report