如何使用KnpMenuBundle和Symfony3在MenuBuidler中合并路由参数以获得语言切换器功能

时间:2017-08-25 21:58:06

标签: php twitter-bootstrap symfony knpmenubundle knpmenu

简介

在我使用的个人项目中:

  • XAMPP with PHP v7.1.6
  • Symfony v3.3.6
  • KnpMnenuBundle dev-master / 2.2.x-dev [link 1][link 2][link 3]以管理菜单。
  • Bootstrap v3.3.7
  • Bootstrap和KnpMenuBundle集成[link 4]

设置

在[2],[3]和代码示例[4]中设置我使用过的文档。我的菜单正常运行,BootstrapKnpMenuBundle之间的集成也有效。

问题

为了实施language switcher,我正在使用twig模板。

它工作正常,但我想采用更具伸缩性的方法 - 即 - 使用专用捆绑(KnpMenuBundle)。

目前MenuBuilder会产生丑陋的网址(显示在code sample 1的结果部分中)。我希望它生成code sample 2的结果部分中显示的链接。

我不知道如何实现这一点。

问题

如何使用MenuBuilder生成漂亮的URL?

如何强制合并MenuBuilder中的路线参数?

代码:样本1

我的MenuBuilder

的样本
$language['Language']->setChildrenAttribute("class", "dropdown-menu")
    ->addChild('Language LV', array('label' => 'language.lv', 'route' => 'homepage_lv', 'routeParameters' => array('_locale' => 'lv')))
    ->setExtra('translation_domain', 'language');
$language['Language']->setChildrenAttribute("class", "dropdown-menu")
    ->addChild('Language EN', array('label' => 'language.en', 'route' => 'homepage_en', 'routeParameters' => array('_locale' => 'en')))
    ->setExtra('translation_domain', 'language');
$language['Language']->setChildrenAttribute("class", "dropdown-menu")
    ->addChild('Language RU', array('label' => 'language.ru', 'route' => 'homepage_ru', 'routeParameters' => array('_locale' => 'ru')))
    ->setExtra('translation_domain', 'language');

生成以下链接:

不会发生区域设置更改

代码:样本2

传统的Menu树枝模板

<li><a href="{{ path(current_route, app.request.get('_route_params') | merge({'_locale': 'lv'})) }}">{{ "language.lv" | trans({}, "language") }}</a></li>
<li><a href="{{ path(current_route, app.request.get('_route_params') | merge({'_locale': 'en'})) }}">{{ "language.en" | trans({}, "language") }}</a></li>
<li><a href="{{ path(current_route, app.request.get('_route_params') | merge({'_locale': 'ru'})) }}">{{ "language.ru" | trans({}, "language") }}</a></li>

生成以下链接:

实际上在点击后改变了语言环境

代码:样本3

我的routing.yml文件的相关部分

# redirecting home
homepage:
    path: /
    defaults:
        _controller: 'FrameworkBundle:Redirect:redirect'
        route: home
        permanent: true

# redirecting home
homepage_lv:
    path: /lv/
    defaults:
        _controller: 'FrameworkBundle:Redirect:redirect'
        route: home
        permanent: true

# redirecting home
homepage_en:
    path: /en/
    defaults:
        _controller: 'FrameworkBundle:Redirect:redirect'
        route: home
        permanent: true

# redirecting home
homepage_ru:
    path: /ru/
    defaults:
        _controller: 'FrameworkBundle:Redirect:redirect'
        route: home
        permanent: true

home:
    path:     /{_locale}/home
    defaults: { _controller: 'AppBundle:Home:home' }
    requirements:
        _locale: lv|en|ru

结论

请告知。

感谢您的时间和知识。

1 个答案:

答案 0 :(得分:0)

所以看起来routes parameters MenuBuilder routestwig模板中的redirects)处理routing.yml } $language['Language']->setChildrenAttribute("class", "dropdown-menu") ->addChild('Language LV', array('label' => 'language.lv', 'route' => 'home', 'routeParameters' => array('_locale' => 'lv'))) ->setExtra('translation_domain', 'language'); $language['Language']->setChildrenAttribute("class", "dropdown-menu") ->addChild('Language EN', array('label' => 'language.en', 'route' => 'home', 'routeParameters' => array('_locale' => 'en'))) ->setExtra('translation_domain', 'language'); $language['Language']->setChildrenAttribute("class", "dropdown-menu") ->addChild('Language RU', array('label' => 'language.ru', 'route' => 'home', 'routeParameters' => array('_locale' => 'ru'))) ->setExtra('translation_domain', 'language');

这意味着我的案例的正确语法是:

    >oc new-app https://github.com/<repo>
--> Found image da99a88 (4 weeks old) in image stream "openshift/nodejs" under tag "6" for "nodejs"

    Node.js 6
    ---------
    Node.js 6 available as docker container is a base platform for building and running various Node.js 6 applications and frameworks. Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

    Tags: builder, nodejs, nodejs6

    * The source repository appears to match: nodejs
    * A source build using source code from https://github.com/<repo> will be created
      * The resulting image will be pushed to image stream "appname:latest"
      * Use 'start-build' to trigger a new build
      * WARNING: this source repository may require credentials.
                 Create a secret with your git credentials and use 'set build-secret' to assign it to the build config.
    * This image will be deployed in deployment config "appname"
    * Port 8080/tcp will be load balanced by service "appname"
      * Other containers can access this service through the hostname "appname"
--> Creating resources ...
    imagestream "appname" created
    buildconfig "appname" created
    deploymentconfig "appname" created
    service "appname" created
--> Success
    Build scheduled, use 'oc logs -f bc/<repo>' to track its progress.
    Run 'oc status' to view your app.

它会产生漂亮的网址。