Creating your own Symfony2 Bundle

symfony

Symfony2 is a great web framework. OpenSky is built on this framework and we are one of the largest contributors to it. The primary building block for Symfony2 is a bundle. Through it’s bundle system Symfony 2.0 achieves a level of modularity I haven’t seen in other web frameworks. A bundle permits a developer to add functionality to the framework and is the best way to develop applications with Symfony2. In this post I’ll show you how to create your own bundle.

Start off with the sandbox

First install symfony sandbox…

the console can create a basic bundle for you.

> app/console init:bundle Spf13/SteveBundle

Initializing bundle "SteveBundle" in "Sites/sandbox/src/Spf13"

Tell Symfony2 about it

Just having the bundle in place isn’t enough, you need to tell Symfony2 it exists (and where).

In your app/AppKernel.php inside the \$bundles array in theregisterBundle function add the following line

    new Spf13\SteveBundle\SteveBundle(),

Routing

You also need to define a route and tell Symfony2 where to find the routing file. Of the 4 different formats available, I prefer yaml which is the default in Symfony2.

in app/config/routing.yml

steve:
    resource: @SteveBundle/Resources/config/routing.yml

Now create that routing.yml file with the following contents.

 steve:
     pattern:  /steve
          defaults: { _controller: SteveBundle:Default:index }

The Controller

The init command has created a controller for you called DefaultController with a basic action (index) which renders a basic php template file.

Now visit the page in a browser to confirm everything worked.

http://localhost/[webroot]/app_dev.php/steve

and be greeted by your new bundle.

Related Posts