This tutorial has a new version, check it out!

Generating URLs

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.

We now have two pages: the HTML /genus/{genusName} page and the JSON endpoint. Ok ok, the JSON endpoint isn't really a page, at least not in the traditional sense. But pretend it is for a second. Pretend that we want to link from the HTML page to the JSON endpoint so the user can see it. Yes yes, we're going to do something fancier with JavaScript in a minute, but stay with me: this is important.

In show.html.twig, get rid of all this notes stuff because we're not passing in a notes variable anymore. Instead, near the top, add a new anchor tag. I want to put the URL to the getNotesAction page. Fill this in with /genus/{{ name }}/notes.

Perfect right! Wrong! Ok, only kind of wrong. This will work: you can click the link and go that URL. But this kinda sucks: if I decided that I needed to change the URL for this route, we would need to hunt down and update every link on the site. That's ridiculous.

Instead, routing has a second purpose: the ability to generate the URL to a specific route. But to do that, you need to give the route a unique name. After the URL, add comma and name="genus_show_notes":

... lines 1 - 10
class GenusController extends Controller
... lines 13 - 22
* @Route("/genus/{genusName}/notes", name="genus_show_notes")
* @Method("GET")
public function getNotesAction($genusName)
... lines 29 - 38

The name can be anything, but it's usually underscored and lowercased.

The Twig path() Function

To generate a URL in Twig, use the path() function. This has two arguments. The first is the name of the route - genus_show_notes. The second, which is optional, is an associative array. In Twig, an associative array is written using { }, just like JavaScript or JSON. Here, pass in the values for any wildcards that are in the route. This route has {genusName}, so pass it genusName set to the name variable:

... lines 1 - 4
{% block body %}
<h2 class="genus-name">{{ name }}</h2>
<a href="{{ path('genus_show_notes', {'genusName': name}) }}">Json Notes</a>
... lines 9 - 27
{% endblock %}

Ok, go back and refresh! This generates the same URL... so that might seem kind of boring. But if you ever need to change the URL for the route, all the links would automatically update. When you're moving fast to build something amazing, that's huge.

Linking to a JSON endpoint isn't realistic. Let's do what we originally planned: use JavaScript to give us a dynamic frontend.

Leave a comment!

  • 2017-10-17 Chuying He

    [thumbs up]

  • 2017-10-16 Diego Aguiar

    Hey Chuying He

    That worked because you passed a string (name) to the route, instead of the variable, I believe your template is missing the "name" variable


  • 2017-10-16 Chuying He

    I had a problem with this session too. Wenn I wrote the code(exactly like in video), the web keeping telling me "No route found for GET /../". I resolved it by a little change in show.html.twig:
    Orginal: < a href="{{ path('genus_show_notes', {'genusName':name} ) }}">Json Notes

    Change to: < a href="{{ path('genus_show_notes', {'genusName':'name'} ) }}">Json Notes

    (Add ' ' an name, holp it will help)

  • 2017-08-07 Victor Bocharsky

    Hey Oskaras,

    Great catch! Actually, path() Twig function solves such kind of problems very well, you would love to use it further instead of compound URLs manually:
    {{ path('genus_show_notes', {'genusName': name}) }}


  • 2017-08-05 Oskaras Serelis

    I found it. Problem was in show.html.twig must be -"
    "/genus/{{ name }}/notes">JSON notes".
    My mistake was -"
    "/genus{{ name }}/notes">JSON notes". Anyway thank you for help.

  • 2017-08-04 Oskaras Serelis

    No route found for "GET /genusnotes/notes" (from "http://localhost:8000/genus/notes")and when I move my mouse to $genusName shows - "Unused parameter"

  • 2017-08-04 Diego Aguiar

    Hey Oskaras Serelis

    Look's like you are missing "genusName" parameter in your "getNoteAction" method, change it to this:

    * @Route("/genus/{genusName}/notes", name="genus_show_notes")
    * @Method("GET")
    public function getNotesAction($genusName) {
  • 2017-08-04 Oskaras Serelis


    namespace AppBundle\Controller;

    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\JsonResponse;

    class GenusController extends Controller
    * @Route ("/genus/{genusName}")

    public function GenusController($genusName)
    return $this->render('genus/show.html.twig',


    * @Route("/genus/{genusName}/notes", name="genus_show_notes")
    * @Method("GET")

    public function getNotesAction()
    $notes = [
    ['id' => 1, 'username' => 'AquaPelham', 'avatarUri' => '/images/leanna.jpeg', 'note' => 'Octopus asked me a riddle, outsmarted me', 'date' => 'Dec. 10, 2015'],
    ['id' => 2, 'username' => 'AquaWeaver', 'avatarUri' => '/images/ryan.jpeg', 'note' => 'I counted 8 legs... as they wrapped around me', 'date' => 'Dec. 1, 2015'],
    ['id' => 3, 'username' => 'AquaPelham', 'avatarUri' => '/images/leanna.jpeg', 'note' => 'Inked!', 'date' => 'Aug. 20, 2015'],

    $data =[
    'notes' => $notes

    return new JsonResponse($data);



    {% extends 'base.html.twig' %}

    {% block title %}Genus {{ name }}{% endblock %}

    {% block body %}
    <h2 class="genus-name">{{path('genus_show_notes', {'genusName':name}) }}</h2>

    JSON notes
    <div class="sea-creature-container">
    <div class="genus-photo"></div>
    <div class="genus-details">
    <dl class="genus-details-list">
    <dt>Known Species:</dt>
    <dt>Fun Fact:</dt>
    <dd>Octopuses can change the color of their body in just three-tenths of a second!</dd>
    <div class="notes-container">
    <h2 class="notes-header">Notes</h2>
    <section id="cd-timeline">

    {% endblock %}

  • 2017-08-03 weaverryan

    Hey Oskaras Serelis!

    Hmm... this seems very funny :/. Can you post your @Route code and also the code where you generate the link from your template? Something definitely isn't quite right.... but I'm not sure what yet.


  • 2017-08-03 Oskaras Serelis

    Hello, I have problem. When i press link to Json, it shows

    No route found for "GET /genussgs/notes" (from "http://localhost:8000/genus/sgs")

    404 Not Found - NotFoundHttpException

    1 linked Exception:

    ResourceNotFoundException » . I am writing - "http://localhost:8000/genus/sgs" , and then I am having -"http://localhost:8000/genussgs/notes". If I not use link -"http://localhost:8000/genus/sgs/notes" it works perfectly.