I was struggling to add prefix in my route with KnpMenu on Symfony 3.0, my prefix being the language ISO initials.
I was getting an error message similar to that: Some mandatory parameters are missing ("_locale") to generate a URL for route "home"
Found a solution, I share it with you:
In my routing file I had:
#[my_project]\app\config\routing.yml
my_app:
resource: "@MyBundle/Resources/config/routing.yml"
prefix: /{_locale}
requirements:
_locale: fr|en
In my sub routing file:
#[my_project]\src\MyBundle\Resources\config\routing.yml
home:
path: /home
defaults: {_controller:MyBundle:Home:index}
I implemented the menu as explained in the doc (see: https://github.com/KnpLabs/KnpMenu)
#[my_project]\MyBundle\Menu\Builder.php
namespace MyBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class Builder implements ContainerAwareInterface{
use ContainerAwareTrait;
public function mainMenu(FactoryInterface $factory, array $options){
$menu = $factory->createItem('root');
$logger = $GLOBALS['kernel']->getContainer()->get('logger');
$logger->info('in mainMenu');
$session = $this->container->get('session');
$locale = $session->get('_locale');
$routeParameters = array('_locale'=>$locale);
$menu->addChild('home', array('routeParameters' => $routeParameters,'route' => 'home'));
return $menu;
}
}
The solution to my problem was to implement the routeParameters array inside the array parameter required by the method addChild(): $menu->addChild('home', array('routeParameters' => $routeParameters,'route' => 'home'));