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

AppBundle, Routing and Annotations

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Now, we need to build a route and create a page. But I want to do this with as few files as possible. Remember, we're going for micro here!

In Symfony, your PHP code lives in a bundle. But actually, you don't need a bundle at all: you could code entirely without one. And this could give us one less file.

In practice, not having a bundle complicates a few things. So, we will create one here, but if you're curious about the approach, ask me in the comments, or read our AppBundle blog post that talks about this.

Creating AppBundle

Create a new directory called just AppBundle - not src/AppBundle - I want to eliminate some directories! Put a new PHP class in there also called AppBundle in an AppBundle namespace. Make this class extend Bundle:

... lines 1 - 2
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
}

Activate this in AppKernel with new AppBundle/AppBundle():

25 lines AppKernel.php
... lines 1 - 5
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
... lines 11 - 13
new \AppBundle\AppBundle()
);
... lines 16 - 17
}
... lines 19 - 23
}

Routing

Cool - that's the last time you'll need to do that. Now let's create a page. Open routing.yml. To minimize the number of files I need to touch I'll use annotation routing. If you prefer YAML, you can start adding your routes here, point them to controller classes and be on your way.

For us, add an import: resource: @AppBundle/Controller with type: annotation:

app_annotations:
resource: "@AppBundle/Controller"
type: annotation

In AppBundle, create the Controller directory and a new class in there called MighyMouseController in honor of another small, but powerful thing.

... lines 1 - 2
namespace AppBundle\Controller;
... lines 4 - 7
class MightyMouseController
{
... lines 10 - 16
}

Add public function rescueAction() and return a new Response(): "Here I come to save the day!":

... lines 1 - 4
use Symfony\Component\HttpFoundation\Response;
... lines 6 - 7
class MightyMouseController
{
... lines 10 - 12
public function rescueAction()
{
return new Response('Here I come to save the day!');
}
}

For routing, it's like normal: add the use statement for Route. Then above the method, finish things with @Route("/"):

... lines 1 - 5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
... line 7
class MightyMouseController
{
/**
* @Route("/")
*/
public function rescueAction()
{
... line 15
}
}

Autoloading!

Let's see if things work. Error! Where are you when we need you Mighty Mouse?

Attempted to load class "AppBundle" from namespace AppBundle.

The autoloader doesn't know that we have classes living inside the AppBundle directory. In a traditional Symfony project, there's a pre-made autoload section in composer.json that takes care of this for us. We on the other hand need to do this manually.

Add an autoload section, with a psr-4 section below it. Inside, we need just one entry AppBundle\\ mapped to AppBundle/:

19 lines composer.json
{
... lines 2 - 12
"autoload": {
"psr-4": {
"AppBundle\\": "AppBundle/"
}
}
}

This says that if a class's namespace starts with AppBundle, look for it inside the AppBundle directory. After that, it follows the same namespace rules as always. You can even rename AppBundle to something else like src - this is the only line you'd need to change.

To get the change to take effect, run composer dump-autoload:

composer dump-autoload

You don't normally need to run this - it happens after a composer install or update.

Autoloading Annotations

Try it again. Excellent - the next error!

The annotation "@Sensio\Bundle\FrameworkExtraBundle\Configuration\Route"
... does not exist.

It doesn't see the Route annotation class - it's almost as if we forgot the use statement. If you use annotations in a project, you need one extra autoload line.

In the config/ directory, create an autoload.php file. Go back to the Standard Edition code so we can cheat off of it. Find app/autoload.php and copy its contents. Paste that here:

... lines 1 - 8

I'll delete some comments to make things really small. This includes the normal autoloader, then adds an extra thing for annotations. Now, in index.php - or anywhere else you need to autoload - require this file instead:

24 lines web/index.php
... lines 1 - 10
$loader = require_once __DIR__.'/../config/autoload.php';
... lines 12 - 24

Refresh! Very nice! Let's count the files: 1, 2, 3, 4, 5, 6, 7. We're rocking the Symfony framework with basically less than 10 files.

Leave a comment!

8
Login or Register to join the conversation
Default user avatar
Default user avatar Никита | posted 5 years ago

Many thanks! You save my time and life :) This line many helped me: "composer dump-autoload"

Reply

Haha, you're welcome! It's the right way to avoid ineligible `composer update` call ;)

Cheers!

Reply
Default user avatar
Default user avatar Karthik | posted 5 years ago

Hello,

When I add line "use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;" in MightMouseController.php and Save it this line gets removed automatically . I have also added autoload.

Can you please let me know where I am going wrong?

Thanks!

Reply

Hey Karthik!

That is *strange*! So literally, you add that line to MightyMouseController.php, and then it suddenly disappears? Nothing in Symfony should be modifying your files... so it shouldn't be anything strange coming from Symfony. What editor are you using? When you reload the page, do you see an error?

Cheers!

Reply
Default user avatar

Thanks for reply. Yes it disappears when i save it and I am using Sublime.

I see error "/ - No such file or directory"

Thanks!

Reply

Hi Karthik!

Hmm, so it seems we have two issues:

1) For some reason, when you save a file in Sublime... something removes part of your code after. This, is totally a mystery

2) The error - "/ No such file or directory" - doesn't seem like an error that's coming from Symfony. Are you using the built-in PHP web server? What does the error look like exactly (maybe take a screenshot)? This error might just be coming from your web server - saying that it can't find the PHP file to execute.

Cheers!

Reply
Default user avatar
Default user avatar Karthik | weaverryan | posted 5 years ago | edited

Hi weaverryan !

Thanks for your reply and Sorry for the delay.

How do i attach the screenshot here I don't see any options to attach.

Thanks!

Reply

Hi Karthik,

We disable the option to upload images directly with Disqus, but you can attach a link to uploaded images - just use any service which allows image sharing like Imgur etc.

Cheers!

Reply
Cat in space

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

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "symfony/symfony": "^2.7", // v2.7.2
        "sensio/framework-extra-bundle": "^3.0", // v3.0.9
        "vlucas/phpdotenv": "^2.0", // v2.0.1
        "symfony/monolog-bundle": "^2.7" // v2.7.1
    }
}
userVoice