Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Bundles of Joy!

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Bundles are a hipster buzzword in the Symfony world. Yea, they're cool but we really deserve all the credit! A bundle is just a place for us to put our hard-earned code. We might make an EventBundle directory for that feature and a UserBundle where we build the registration and login stuff.

We'll put anything and everything into a bundle: PHP code, config, templates, CSS and cats. We can also put other people's bundles into our project. A bundle in Symfony is similar to a plugin in other systems, but, ya know, way more hipster.

The Console

Yes, we can create bundles manually. But I'd rather have someone else do it for me. Meet console: a magic executable file in the app/. Run it to see all of the tricks it knows:

php app/console

Woh! All those green words are different console commands, including a lot things that help you work with the database and debug. I like tools as much as any programmer geek, so we'll use a lot of these over time.

Generating the EventBundle

For now run the generate:bundle command:

php app/console generate:bundle

For the bundle namespace, type Yoda/EventBundle. A bundle namespace always has two parts: a vendor name and a name describing the bundle. In honor of the Jedi master, we'll use "Yoda" for the first part and EventBundle for the second. Unless you also work for Yoda, you'll probably use your company or project name instead. Keep these as short as possible to save typing later.

Next, it wants a nickname for our bundle. We're going to be writing this a lot, and lets face it, we're busy people. So, let's choose something short, like EventBundle. The only rule is that this ends with Bundle.

Use the target default directory, but choose yml as the configuration format. You'll just have to trust me on this part - we'll check out the annotation configuration format later.

For the rest of the questions, just hit the enter key wildly. And once the console-gnomes are finished, we have a brand new bundle.

What the Generator Did

This did exactly three things for us.

First, it made a src/Yoda/EventBundle directory with some sample bundle files.

Second, it plugged our bundle into the motherboard by adding a line in the AppKernel class.

Third, it added a line to the routing.yml file that imports routes from the bundle. Contain your excitement: we're about 30 seconds from talking about this part.

The PHPStorm Symfony Plugin

But I want to share a quick secret first. If you're using PHPStorm like I am, I need you to download an aewsome Symfony plugin. For everyone else, this is totally not needed, it just adds some shortcuts.

Once it's installed, you need to activate it. We're now super-charged with a ton of Symfony-specific help. You'll see this along the way.

Leave a comment!

2
Login or Register to join the conversation
Default user avatar

It is a pity the bundle namespace and bundle name were given the same name. If they weren't is would be easier to distinguish them later.
Also I don't get an option for a Bundle namespace so I can't put in a vendor i.e. Yoda. I am only asked for 'Bundle name:'.
It is strange that the bundles which are essentially apps are in the src and not app directories. Doesn't seem intuitive.

Reply

Hey Shane!

Yea, I agree - this definitely leads to "what is the bundle name?" versus "what is the namespace"? Starting in later versions of Symfony, we've simplified this: we now recommend that your app just has *one* bundle: AppBundle. We cover that in the latest version of this tutorial: http://knpuniversity.com/sc.... But, you can of course do whatever you want :).

And that's also why you see differences in this command: once we started recommending AppBundle we (actually I did this) updated the command to be more user-friendly and also to push people a bit more towards the idea of just having one bundle. There is an earlier question asking you if this bundle will be "shared". If you answer "yes" to this, then it will ask you for a vendor namespace. But if you answer no, there's no reason really to have a vendor namespace - it just makes your namespaces / directories a bit deeper. So, that's what's going on there!

And finally - very interesting through bout the app/ directory! This is actually something we considered when we moved to the AppBundle idea! One of the biggest reasons we didn't do this is simply to keep things consistent with how they were in the past. Putting your bundle in app versus src are both valid, but everyone has become accustomed to src, and putting things in app/ doesn't have any extra advantage. But with the AppBundle approach, there is a key delineation between app and src: src/ should contain your PHP code and app/ should contain everything else (templates, configuration, etc).

But, there's a cool addendum to this discussion :). *If* you wanted to put your AppBundle into the app/ directory (so you have app/AppBundle... or even, to that you remove AppBundle and have things simply like app/Controller, app/Entity, etc) you can do this very easy: simply change the autoload in your composer.json to point to app/ instead of src/ https://github.com/symfony/....

Phew! I hope that at least somewhat clarifies! This tutorial is now out-of-date, but also, you're asking (or commenting about) some very insightful questions.

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0" // v2.2.0
    }
}
userVoice