Talk To Us For FREE

We are in the Business of Support and that starts before you even become a client.


  • Please prove you are human by selecting the Cup.

Symfony 2 Best Practices for Structuring Bundles

Categories : Symfony

In Symfony 2 Development, a bundle is a directory that has a structure that is well defined and it can host classes, controllers, web resources and much more. Bundles are very flexible. But it is essential that you are well informed of the best practices to use and distribute them.

Symfony2 Best Practices for Structuring Bundles

Here are some of the best practices while you structure bundles in Symfony 2

Bundle Name

A Bundle is basically a PHP namespace. It must follow the interoperability standards for PHP 5.3 name spaces and class names.
A typical bundle name starts with a vendor segment, a zero or some more category segments and a bundle suffix. A bundle class name has some specific rules to add to it. It allows only alphanumeric charecters, underscores are allowed, CamelCased names, descriptive and short name, prefix should be concatenation of vendor name, suffix with the name bundle.

Directory Structure

Take a look at this example for helloBundle

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

The XXX directory(ies) shows the namespace structure

It’s mandatory to have the following files:

  • HelloBundle.php;
  • Resources/meta/LICENSE: The full license for the code;
  • Resources/doc/index.rst: The root file for the Bundle documentation.

Sub directories depth should be kept minimal. The Bundle directory should be read only. Just in case you need to create temporary files, you can store them under cache/ or log/ directories.

Classes

All classes and files should follow the Symfony 2 coding standards. A Bundle has a name space hierarchy. For example: a HelloController controller is stored in Bundle/HelloBundle/Controller/HelloController.php and the fully qualified class name is Bundle\HelloBundle\Controller\HelloController.

Vendors

In Symfony 2, a bundle should not embed third party PHP libraries or JavaScript, CSS, or any other language. Symfony 2 autoloading should be used instead.

Tests:

A Symfony 2 bundle comes with a test suite written with PHPUnit and stored under the Tests/ directory.

Following are the best practices for tests:

  • must be executable with a simple phpunit
  • should be used to test response output.
  • Should test 95% of the code base.

Controllers

A controller must not extend the controller base class. Instead, they can implement ContainerAwareInterface or extend ContainerAware .

Routing

If a bundle provides routes, they must be prefixed with bundle alias.

Templates

Twig must be used if a bundle provides templates. The main layout must not be provided by a bundle unless it’s a fully working application.

Translation files

Message translations for a bundle must be defines in XLIFF format. Domain name should be after the bundle.

Configuration

Symfony 2 built in mechanisms provide flexibility for configuration. For simple configuration, the default parameters entry will suffice.

Parameters are key value pairs (any valid PHP value). The parameter starts with the bundle alias. Rest of the parameter uses a period to separate different parts.
(e.g.acme_hello.email.from).

Symfony 2 allows the end user can provide values in any configuration file:

•	YAML
1
2
3	# app/config/config.yml
parameters:
acme_hello.email.from: fabien@example.com
[Total: 0    Average: 0/5]

Client Saying

If you're starting a new business or want to upgrade an existing site, I strongly recommend…
Mr. Manjesh Singh
CTO
Daniel Coburn, the president of Bragshare.com gives the honest opinion regarding his experience working with Brainvire…
Mr. Daniel Coburn
Chad Gundry, the CEO of Bragshare.com says that he has worked extensively with Brainvire team since…
Mr. Chad Gundry
The best thing about Brainvire is Compatibility. Whenever we have a query Brainvire is always ready…
Mr Craig Agranoff
Brainvire is a professional company with professional developers who understand the problems that we are facing…
Mr. Clarke Verdel

Latest Happenings

  • 28 Mar 2017
    Brainvire’s Massive Stride by Exhibiting Retail Expertise at High Point Market [ + ]
  • 28 Mar 2017
    Brainvire To Hit It Big at Imagine 2017 With Its Presence [ + ]
Enquiry