Symfony3 无法使用服务中的路由器创建已翻译的路由
Symfony3 can't create translated routes with router in service
我正在尝试在我的主导航中构建一个语言切换器,它是由 KNPMenuBundle. Translations are done with the JMSTranslationBundle 创建的。两者都很好。
我想用我的菜单生成器创建一个语言切换器,但是正确路由的生成让我有些头疼。
这是我的服务:
class MenuService
{
private $factory;
private $translator;
private $router;
public function __construct(FactoryInterface $factory, Translator $translator, Router $router)
{
$this->factory = $factory;
$this->translator = $translator;
$this->router = $router;
}
public function createMainMenu(RequestStack $requestStack, array $languages)
{
// Language Switcher - $languages === ['en', 'de']
$request = $requestStack->getCurrentRequest();
$routeName = $request->get('_route');
$menu->addChild('menu.language', array(
'uri' => '#',
'label' => '<i class=\'fa fa-flag-checkered\'></i> '.$this->translator->trans('menu.language.main'),
'extras' => array('safe_label' => true)
))
->setAttribute('class', 'dropdown singleDrop')
->setChildrenAttribute('class', 'dropdown-menu dropdown-menu-left')
;
foreach ($languages as $language)
{
$menu->getChild('menu.language')->addChild('menu.language.'.$language, array(
'route' => $this->router->generate($routeName, array_merge($request->get('_route_params'), ['_locale' => $language]))
));
}
}
这是我的服务定义
menu_builder:
class: AppBundle\DependencyInjection\MenuService
arguments: ['@knp_menu.factory', '@translator.default', '@jms_i18n_routing.router']
menu.main:
class: Knp\Menu\MenuItem
factory: ['@menu_builder', createMainMenu]
arguments: ['@request_stack', '%locales%']
scope: request
tags:
- { name: knp_menu.menu, alias: main }
如果我注入 JMSTranslationBundle 提供的路由器,我会收到以下错误:
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "/en/" as such route
does not exist.").
如果我使用的是 symfony 的默认路由器,我会收到此错误消息:
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "homepage" as such
route does not exist.").
当我在控制台调试路由器时,这是输出:
en__RG__homepage ANY ANY ANY /en/
de__RG__homepage ANY ANY ANY /de/
必须使用哪个路由器才能使路由正常工作?
我发现我在使用这两个包时犯了错误。
JMS\I18nRoutingBundle\Router::generate()
函数将 return 绝对路径 给函数的路由。
所以会发生以下情况:
$absolutePath = $this->router->generate('homepage', array_merge($request->get('_route_params'), ['_locale' => $language]));
dump($absolutePath); // Will return "/de/" or "/en" in my case
所以,考虑到这一点,我用参数 route
生成的路由显然会失败,因为函数不 return route
但 absolute path
.
鉴于此,这有效:
foreach ($languages as $language)
{
$menu->getChild('menu.language')->addChild('menu.language.'.$language, array(
'uri' => $this->router->generate($routeName, array_merge($request->get('_route_params'), ['_locale' => $language]))
));
}
如果我将 route
更改为 uri
,我的代码将正常工作。
我正在尝试在我的主导航中构建一个语言切换器,它是由 KNPMenuBundle. Translations are done with the JMSTranslationBundle 创建的。两者都很好。
我想用我的菜单生成器创建一个语言切换器,但是正确路由的生成让我有些头疼。
这是我的服务:
class MenuService
{
private $factory;
private $translator;
private $router;
public function __construct(FactoryInterface $factory, Translator $translator, Router $router)
{
$this->factory = $factory;
$this->translator = $translator;
$this->router = $router;
}
public function createMainMenu(RequestStack $requestStack, array $languages)
{
// Language Switcher - $languages === ['en', 'de']
$request = $requestStack->getCurrentRequest();
$routeName = $request->get('_route');
$menu->addChild('menu.language', array(
'uri' => '#',
'label' => '<i class=\'fa fa-flag-checkered\'></i> '.$this->translator->trans('menu.language.main'),
'extras' => array('safe_label' => true)
))
->setAttribute('class', 'dropdown singleDrop')
->setChildrenAttribute('class', 'dropdown-menu dropdown-menu-left')
;
foreach ($languages as $language)
{
$menu->getChild('menu.language')->addChild('menu.language.'.$language, array(
'route' => $this->router->generate($routeName, array_merge($request->get('_route_params'), ['_locale' => $language]))
));
}
}
这是我的服务定义
menu_builder:
class: AppBundle\DependencyInjection\MenuService
arguments: ['@knp_menu.factory', '@translator.default', '@jms_i18n_routing.router']
menu.main:
class: Knp\Menu\MenuItem
factory: ['@menu_builder', createMainMenu]
arguments: ['@request_stack', '%locales%']
scope: request
tags:
- { name: knp_menu.menu, alias: main }
如果我注入 JMSTranslationBundle 提供的路由器,我会收到以下错误:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "/en/" as such route does not exist.").
如果我使用的是 symfony 的默认路由器,我会收到此错误消息:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "homepage" as such route does not exist.").
当我在控制台调试路由器时,这是输出:
en__RG__homepage ANY ANY ANY /en/
de__RG__homepage ANY ANY ANY /de/
必须使用哪个路由器才能使路由正常工作?
我发现我在使用这两个包时犯了错误。
JMS\I18nRoutingBundle\Router::generate()
函数将 return 绝对路径 给函数的路由。
所以会发生以下情况:
$absolutePath = $this->router->generate('homepage', array_merge($request->get('_route_params'), ['_locale' => $language]));
dump($absolutePath); // Will return "/de/" or "/en" in my case
所以,考虑到这一点,我用参数 route
生成的路由显然会失败,因为函数不 return route
但 absolute path
.
鉴于此,这有效:
foreach ($languages as $language)
{
$menu->getChild('menu.language')->addChild('menu.language.'.$language, array(
'uri' => $this->router->generate($routeName, array_merge($request->get('_route_params'), ['_locale' => $language]))
));
}
如果我将 route
更改为 uri
,我的代码将正常工作。