Buy
Buy
This tutorial has a new version, check it out!

Ok, you're a configuration pro: you know where services come from and how to configure them. Here's our question: where do routes come from? And can third party bundles give us more routes?

The answer is, no :(. Ah, I'm kidding - they totally can. In fact, find your terminal and run:

./bin/console debug:router

Surprise! There are a lot more than just the two routes that we've created. Where are these coming from?

Answer time: when Symfony loads the route list, it only loads one routing file. In the dev environment, it loads routing_dev.yml:

_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_errors:
resource: "@TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
_main:
resource: routing.yml

In all environments, it loads routing.yml:

app:
resource: "@AppBundle/Controller/"
type: annotation
... lines 4 - 9

Inside routing_dev.yml, we're importing additional routes from WebProfilerBundle and the TwigBundle:

_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_errors:
resource: "@TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
... lines 12 - 15

That's why there are extra routes in debug:router. This also imports routing.yml at the bottom to load all the main routes:

... lines 1 - 12
_main:
resource: routing.yml

You can really import anything you want: you're in complete control of what routes you import from a bundle. These files are usually XML, but that doesn't make any difference.

In the main routing.yml, there's yet another import:

app:
resource: "@AppBundle/Controller/"
type: annotation
... lines 4 - 9

We're out of control! This loads annotation routes from the controllers in our bundle.

Creating YAML Routes

But you can also create routes right inside YAML - a lot of people actually prefer this over annotations. The difference is purely preference: there's no performance impact to either.

Tip

In a big project, there is actually some performance impact in the dev environment for using annotations.

Let's create a homepage route: use the key homepage - that's its name. Then add path set to just /:

... lines 1 - 4
homepage:
path: /
... lines 7 - 9

Now, we need to point this route to a controller: the function that will render it. Doing this in yml is a bit more work. Add a defaults key and an _controller key below that. Set this to AppBundle:Main:homepage:

... lines 1 - 4
homepage:
path: /
defaults:
_controller: AppBundle:Main:homepage

Yes yes, this looks totally weird - this is a Symfony-specific shortcut. It means that we will have a controller in AppBundle called MainController with a method named homepageAction().

In the Controller directory, create that new MainController class:

... lines 1 - 2
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MainController extends Controller
{
... lines 9 - 12
}

Next add public function homepageAction(). Make this class extend Symfony's base Controller so that we can access the render() function. Set it to render main/homepage.html.twig without passing any variables:

... lines 1 - 6
class MainController extends Controller
{
public function homepageAction()
{
return $this->render('main/homepage.html.twig');
}
}

Create this real quick in app/Resources/views: new file, main/homepage.html.twig:

{% extends 'base.html.twig' %}
{% block body %}
... line 4
{% endblock %}

Templates basically always look the same: extend the base template - base.html.twig - and then override one or more of its blocks. Override block body: the block that holds the main content in base.html.twig.

Add some content to greet the aquanauts:

... lines 1 - 2
{% block body %}
<h1 class="page-header text-center">Welcome Aquanauts!</h1>
{% endblock %}

Done! Before trying this, head over to the terminal and run:

./bin/console debug:router

There it is! There are many ways to define a route, but the result is exactly the same. Refresh the browser. Thank you yml for that lovely brand new route.

Woh guys. This course was a serious step forward. In the next parts, we're going to start adding big features: like talking to a database, forms, security and more. And when we do, I've got some exciting news: because of your work here, it's all going to make sense. Let's keep going and get really productive with Symfony.

Leave a comment!

  • 2018-03-26 Cesar

    Thanks Victor. I will try it on my web.

  • 2018-03-20 Victor Bocharsky

    Hey Cesar,

    Yep, opensky/SitemapBundle looks abandoned and works for Symfony 2.1-2-3 only. But the other one looks maintained and from its composer.json I see it supports Symfony since 2.3.0, and even Symfony 4! So I'd recommend this one as the best among others sitemaps bundles. Btw, we use this one on KnpU as well.

    Cheers!

  • 2018-03-19 Cesar

    Hi guys. I am trying to improve my SEO in my Symfony3 app and I need to create a Sitemap. Do you know a good bundle for that? I have found these http://knpbundles.com/keywo... but I think they work only with Symfony2. If I don't need any bundle, an you give me some orientation in how to build a sitemap in Symfony3? I hope you can help me with this little question.

  • 2017-10-17 Victor Bocharsky

    Hey Cesar,

    Yes, we do! Right now we're releasing "Deploy with Ansistrano" course, check it out: https://knpuniversity.com/s... - it's mostly has been released already. This course is based on Ansible, but if you're new with Ansible - don't worry to much, it's really simple and we explain it mostly from scratch, so I believe you'll figure it out easily. But if you're curious about Ansible more, we do have a screencast for it too: https://knpuniversity.com/s... , but once again, it's not a strict requirement to now Ansible well before start deploying with Ansistrano ;)

    Cheers!

  • 2017-10-17 Cesar

    Hi, do you have any screencast where you explain how to deploy your symphony project in a server? I finished my first website in symfony following your tutorials and now I want to put it in live. I hope you can help me.

  • 2016-11-21 Victor Bocharsky

    Hey Michael,

    Haha, I believe Ryan just did video fast forward here :) But you can check out PhpStorm Live Templates and we have a screencast about it: https://knpuniversity.com/s...

    Cheers!

  • 2016-11-20 Boutboul Michael

    Hey !
    Just a little question about autocompletion : why when you type "h1" you've got bootstrap classes ? Where do you configure that ?
    Best regards
    Michael BOUTBOUL