Search code examples
symfonysymfony-2.1knpmenu

KnpMenu, unable to set current menu item


I have seen the other errors about this problem. I have done the exact same thing. When I try to render the menu I get this Fatal error:

Fatal error: Call to undefined method Knp\Menu\MenuItem::setCurrentUri()
in ProjectBundle/Menu/Builder.php on line 23

This is how my Builder looks:

<?php
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;

class Builder extends ContainerAware
{

private $factory;

public function __construct(FactoryInterface $factory)
{
    $this->factory = $factory;
}

public function createMenu(Request $request)
{

    $menu = $this->factory->createItem('root');
    $menu->setCurrentUri($request->getRequestUri());

    $menu->addChild('Home', array('route' => '_home'));
    $menu->addChild('About', array('route' => '_about'));
    $menu->addChild('Bullshit', array('route' => '_bullshit'));

    return $menu;
}
}

I went through the issue tracker on Github, and it seems this issue has been fixed, but why do I have the same issue again?

I mean, when I var_dump($menu), it clearly says it's a MenuItem and seeing the Documentation of KnpMenu, there is definitely a setCurrentUri() method for my $menu.


Solution

  • It looks like the MenuItem::setCurrentUri() method was deprecated as of v1.1.0. See https://github.com/KnpLabs/KnpMenu/issues/63 for more information. That issue has several links on how to set the current uri of the menu using UrlVoter instead.