Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Best Practices to Structure Bundles for Symfony 2 Framework

Author: Deepa Ranganathan
by Deepa Ranganathan
Posted: Jul 24, 2015

When working with Symfony 2, a bundle is called a directory with a well defined structure. In this directory, you will be able to host classes, controllers, web resources and other categories in a structured fashion. They are pretty flexible. But, you need to know how to structure them so that you don't misuse the whole concept. For this purpose, you should be aware of the best practices. Here are the best practices to structure bundles for Symfony 2.

Naming Convention

When you are talking of bundle, you are basically talking about a PHP namespace. When naming the bundle, you should ideally follow the interoperability standards mentioned by PHP 5.3 with the name spaces and class names. A typical bundle naming begins with vendor segment, a zero, category segments and bundle suffix. There are specific rules associated with naming a bundle. Alphanumeric characters and underscores are allowed in the naming convention. CamelCased names, descriptive and short names, prefix should follow the vendor name.

Directory Structure

Let's say you are creating a bundle "HelloBundle"

XX/...

HelloBundle/

HelloBundle.php

Controller/

Resources/

meta/

LICENSE

config/

doc/

index.rst

translations/

views/

public/

Tests/

In this XX directory, you learn about the namespace structure. You should necessarily include the following files

  • HelloBundle.php
  • Resources/meta/License: The full license for the code
  • Resources/doc/index.rst: Root file for the bundle documentation
  • The depth of your sub directories should be minimum while a read only bundle directory should be existent. You can store temporary files along the following path cache/directories or log/directories

    Classes & Vendors

    You should implement the Symfony 2 coding standards when defining classes and files. You should follow the name space hierarchy

    You should use the autoloading feature instead of embedding third party PHP libraries

    Testing

    The test suite for Symfony 2 is stored in Tests/directory. You should consider the following best practices when working with the test suite
  • Executable with PHPunit
  • Ability to test the response output
  • Ability to test 95% of the code
  • Controllers & Routing

    When indulging in best practices, ensure the controller does not extend the base controller class. You can either implement containerawareinterface or extend containeraware. In case of routing, make sure you have the bundle alias prefixed to it.

    Templates & Translation Files

    If your bundle includes templates, then ensure you have included a twig. You should ideally define the message translations in XLIFF format. Always include the domain name after the bundle.

    Configuration

    You gain flexibility for configuration with Symfony 2. You can enter the default parameters for a simple configuration. Parameters begin with bundle alias and the rest of the parameters use period separators. You can provide values within any part of the configuration line

    YAML

    1

    2

    3 # app/config/config.yml

    parameters:

    acme_hello.email.from: fabien@example.com

    Conclusion:

    When you are creating a bundle for Symfony 2, you need to ensure that the naming convention is in line with the best practices. You should always structure the bundles keeping the controllers, routing, templates, classes, and configuration in mind. Hire Symfony Programmers to induct best practices in your framework.

    About the Author

    Deepa is a technical writer with Semaphore Software.

    Rate this Article
    Leave a Comment
    Author Thumbnail
    I Agree:
    Comment 
    Pictures
    Author: Deepa Ranganathan

    Deepa Ranganathan

    Member since: Mar 29, 2015
    Published articles: 42

    Related Articles