Search code examples
symfonyknpmenubundle

Symfony2 KnpMenuBundle - Following tutorial and came across this error


I followed this tutorial:

https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md#installation

And have come across the following error:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "page_show" as such route does not exist.") in /var/www/bundles/src/Acme/DemoBundle/Resources/views/Default/index.html.twig at line 4.

Is there a step I am missing here to pass something to a controller?

From link:

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;

class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
    $menu = $factory->createItem('root');

    $menu->addChild('Home', array('route' => 'homepage'));
    $menu->addChild('About Me', array(
        'route' => 'page_show',
        'routeParameters' => array('id' => 42)
    ));
    // ... add more children

    return $menu;
}
}

To actually render the menu, just do the following from anywhere in any Twig template:

{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu') }}

Solution

  • Following the tutorial exactly, this error is caused by line 25 in the file

    2  // src/Acme/MainBundle/Menu/MenuBuilder.php
       ...
    25         $menu->addChild('Home', array('route' => 'homepage'));
    

    The tutorial code assumes that you have a route called 'homepage'. Assuming you set this up inside a custom Bundle, then a quick way to solve this problem so you can get the tutorial up and running is to go to...

    // src/Acme/MainBundle/Resources/config/routing.yml
    

    ...and copy the homepage route from there (will look something like acme_main_bundle_homepage)