我很难在Symfony 3.0上使用KnpMenu在我的路线中添加前缀,我的前缀是ISO首字母语言。
我收到类似的错误消息: 缺少一些必修参数(" _locale")以生成路线的网址" home"
找到解决方案,我与您分享:
在我的路由文件中,我有:
#[my_project]\app\config\routing.yml
my_app:
resource: "@MyBundle/Resources/config/routing.yml"
prefix: /{_locale}
requirements:
_locale: fr|en
在我的子路由文件中:
#[my_project]\src\MyBundle\Resources\config\routing.yml
home:
path: /home
defaults: {_controller:MyBundle:Home:index}
我按照文档中的说明实现了菜单(参见: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;
}
}
答案 0 :(得分:0)
我的问题的解决方案是在方法addChild()所需的数组参数中实现routeParameters数组: $ menu-> addChild('home',array('routeParameters'=> $ routeParameters ,'route'=>'home'));