4

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.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
tolgap
  • 9,629
  • 10
  • 51
  • 65

4 Answers4

3

It should doesn't work if the probleme is due to the request . You can try that:

 public function **CreateMenu**(\Knp\Menu\FactoryInterface $factory, array $options)
{   $menu = $factory->createItem('root');

    $menu->setCurrentUri($this->container->get('request')->getRequestUri());
$menu->addChild('Home', array('route' => '_home'));
$menu->addChild('About', array('route' => '_about'));
..

return $menu;}
aurny2420289
  • 481
  • 5
  • 16
3

@aurny2420289 gives a simple solution.

But now it's suggested to use a UriVoter

use Knp\Menu\Matcher\Voter\UriVoter;
use Knp\Menu\Renderer\ListRenderer;

//...

$matcher = new Matcher();
$matcher->addVoter(new UriVoter($_SERVER['REQUEST_URI']));

$renderer = new ListRenderer($matcher);

//...
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
1

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.

theunraveler
  • 3,254
  • 20
  • 19
  • You were right, I haven't been able to find that out myself. I read on a couple of answer on SO that `setCurrentUri()` should work. I did a RequestVoter and implemented VoterInterface. It works now. See [this fix on Github](https://github.com/KnpLabs/KnpMenuBundle/issues/122#issuecomment-6563863) – tolgap Sep 10 '12 at 17:10
0

I found the simplest solution. just add:

$menu->setCurrent(true);

It adds class 'current' to current menu. Method setCurrentUri has been deprecated and later removed from knpmenu.

jekaby
  • 403
  • 3
  • 13