21.10.2015 Views

1-33

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Symfony2 – Franz Jordán 2011<br />

Start by creating a src/Acme/TestBundle/ directory and adding a new file<br />

calledAcmeTestBundle.php:<br />

// src/Acme/TestBundle/AcmeTestBundle.php<br />

namespace Acme\TestBundle;<br />

use Symfony\Component\HttpKernel\Bundle\Bundle;<br />

class AcmeTestBundle extends Bundle<br />

{<br />

}<br />

The name AcmeTestBundle follows the standard Bundle naming conventions. You could also<br />

choose to shorten the name of the bundle to simply TestBundle by naming this<br />

class TestBundle (and naming the file TestBundle.php).<br />

This empty class is the only piece you need to create the new bundle. Though commonly empty,<br />

this class is powerful and can be used to customize the behavior of the bundle.<br />

Now that you've created the bundle, enable it via the AppKernel class:<br />

// app/AppKernel.php<br />

public function registerBundles()<br />

{<br />

$bundles = array(<br />

// ...<br />

}<br />

// register your bundles<br />

new Acme\TestBundle\AcmeTestBundle(),<br />

);<br />

// ...<br />

return $bundles;<br />

And while it doesn't do anything yet, AcmeTestBundle is now ready to be used.<br />

And as easy as this is, Symfony also provides a command-line interface for generating a basic<br />

bundle skeleton:<br />

php app/console generate:bundle --namespace=Acme/TestBundle<br />

The bundle skeleton generates with a basic controller, template and routing resource that can be<br />

customized. You'll learn more about Symfony2's command-line tools later.<br />

Whenever creating a new bundle or using a third-party bundle, always make sure the bundle has<br />

been enabled in registerBundles(). When using thegenerate:bundle command, this is<br />

done for you.<br />

Bundle Directory Structure<br />

The directory structure of a bundle is simple and flexible. By default, the bundle system follows<br />

a set of conventions that help to keep code consistent between all Symfony2 bundles. Take a<br />

look atAcmeHelloBundle, as it contains some of the most common elements of a bundle:<br />

Controller/ contains the controllers of the bundle (e.g. HelloController.php);<br />

Resources/config/ houses configuration, including routing configuration<br />

(e.g.routing.yml);<br />

Resources/views/ holds templates organized by controller name<br />

(e.g.Hello/index.html.twig);<br />

41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!