简体   繁体   中英

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') }}

Do a ./app/console router:debug - it will show you all the routes registered in your application. I am guessing page_show is not one of them.

The documentation you are using probably expects you to add your own routes/pages to the menu like this:

$menu->addChild('Home', array('route' => 'homepage'));

Where 'homepage' has to already exist. So does 'show_page'. So you need a controller somewhere that handles a request to the show_page route, or exchange show_page for a route that you have already defined in your app. Hope I made sense.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM