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

Using the slug in the Event URL

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 $12.00

Using the slug in the Event URL

We’ve got slugs! So let’s enjoy them by putting them into our URLs!

First, change the event_show route to use the slug instead of the id:

# src/Yoda/EventBundle/Resources/config/routing/event.yml
# ...

    pattern:  /{slug}/show
    defaults: { _controller: "EventBundle:Event:show" }

# ...

You can also update the other routes if you want to - but this is the most important URL to get right.

Update the showAction accordingly and query for the Event using the slug:

// src/Yoda/EventBundle/Controller/EventController.php
// ...

public function showAction($slug)
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('EventBundle:Event')
        ->findOneBy(array('slug' => $slug));

    // ...

    // also change this line, since the $id variable is gone
    $deleteForm = $this->createDeleteForm($entity->getId());
    // ...

And with those 2 small changes, this page should work!

Updating the URL generation

Head over to the homepage to try it. Ah, a huge error:

An exception has been thrown during the rendering of a template (“Some mandatory parameters are missing (“slug”) to generate a URL for route “event_show”.”)

The event_show route now has a slug wildcard instead of id. So wherever we’re generating a URL to this route, we need to change the wildcard we’re passing to it.

I’ll use the “git grep” command to figure out where we’re using this route:

git grep event_show

Update each to pass in the slug instead of the id:

// src/Yoda/EventBundle/Controller/EventController.php
// ..

public function createAction(Request $request)
    // ...

    return $this->redirect($this->generateUrl(
        'event_show', array('slug' => $entity->getSlug())
{# src/Yoda/EventBundle/Resources/views/Event/index.html.twig #}
{# ... #}

<a href="{{ path('event_show', {'slug': entity.slug}) }}">{{ entity.name }}</a>
{# src/Yoda/EventBundle/Resources/views/Event/edit.html.twig #}
{# ... #}

<a class="link" href="{{ path('event_show', {'slug': entity.slug}) }}">show event</a>

Refresh the homepage. Nice! When we click on an event, we have a beautiful URL.

Leave a comment!

Login or Register to join the conversation
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
        "ircmaxell/password-compat": "~1.0.3", // 1.0.3
        "phpunit/phpunit": "~4.1", // 4.1.0
        "stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0