我正在努力在 Symfony 3.0 上使用 KnpMenu 在我的路由中添加前缀,我的前缀是语言 ISO 首字母。
我收到类似的错误消息: 缺少一些强制参数(“_locale”)以生成路由“home”的 URL
找到了解决办法,分享给大家:
在我的路由文件中,我有:
#[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;
}
}