Part # 2 Bundles and Files Structures

What is a Symfony Bundle and review of files structure

What is a Symfony Bundle?

In this new version of Symfony we will not talk about modules or plugins, from now on it is only bundles

A bundle is a directory that holds everything necessary to implement a feature that could be reusable in other projects in the future.

For example we can have a bundle called "CmsBundle" that will handle a CMS system for our web site with articles, comments and users.
Another bundle could be \\\"NewsBundle\\\" that could handle a little news system. As you can see thay are like packages.

Bundle namespaces

Each bundle is hosted under a namespace, that reminds me of Java, so the namespace should begin with your company name or your client name, followed by a sub-name or category and the name of the bundle itself

Bundles name conventions

All classes are referenced under a namespace like MyCompanyName/MyBundle/Entity.
All classes should specify their bundle name, so the first line of a class file should be:

namespace My\MyBundle\Folders;


Then if you want to use a class from a namespace in another class:

use My\MyBundle\Folders;


You can use as many folders as you want but the bundle name will always be the same, you can create a bundle like: My\MyBundle\Folders or a bundle like: My\MyBundleFolders but in both cases the class name will be: MyMyBundleFolders

Onk let's generate a bundle:

php app/console generate:bundle


Let's say that we want the bundle
My/HelloBundle

So for the

Bundle namespace:My/HelloBundle
Bundle name: MyHelloBundle
Target directory:
Configuration format (yml, xml, php, or annotation): annotation
Do you want to generate the whole directory structure?: yes
Do you confirm generation?: yes
Confirm automatic update of the Kernel?: yes
Confirm automatic update of the Routing?: yes

Now you can chek the app/AppKernel.php file and you will see

// ..
new My\HelloBundle\MyHelloBundle()
);

 

Directory structure for the Bundle

My
---HelloBundle
    ---Controller
    ---DependencyInjection
    ---Resources
    |   |---config
    |   |---doc
    |   |---public
    |   |   |---css
    |   |   |---images
    |   |   ---js
    |   |---translations
    |   |---views
    |       ---Default
    ---Tests
        ---Controller

You can check on https://github.com/search?langOverride=&language=PHP&q=bundle&repo=&start_value=1&type=Repositories&x=1&y=23
for bundles already developped that you can use

Delete a Bundle

Ok, you've create a HelloWorld Bundle and how do you delete it?

  1. 1. Delete the bundle source folder: src/My
  2. 2. Delete the bundle call in the app/AppKernel.php file
  3. 3. Delete the routing assiciated to this bundle in the file: app/config/routing_dev.yml, see app/config/routing.yml


[Back to Learn Symfony]