Symfony 2:自动生成的导航菜单 [英] Symfony 2: Autogenerated navigation menus

查看:151
本文介绍了Symfony 2:自动生成的导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道一个很好的原理,或者也许知道一堆在symfony2中处理导航菜单的方法吗?

Anyone know a good principle or maybe a bundle for dealing with navigation menus in symfony2?

我已经尝试过Knp捆绑包,但这只是设置器和获取器.尽管具有控制器将与我的主菜单匹配的功能,但操作将与子菜单匹配,并且其他参数可以映射到子子菜单.

I've tried the Knp bundle, but it's just setters and getters. I though of a functionality where controllers will match my main menu, actions will match the submenu and additional parameters could be mapped to a sub-sub menu.

我认为我可以使我的应用程序在主菜单中的每个条目都与一个XController.php相关,并且在其子菜单中的每个条目都将与同一控制器中的一个xAction相关. Knp非常灵活,但是(据我了解)不支持这种映射.

I thought that I could make my application that each entry in my main menu will relate to a XController.php and each entry in the submenu under it will relate to an xAction in the same controller. Knp is quite flexible, but (as far as I understand) does not support such mapping.

当我们在相关Controller中时,无论操作或任何其他参数,主导航条目均应处于活动状态.这就是问题所在.在symfony的配置中,您有_controller: ABBundle:NameController:nameAction,从中提取控制器和操作名称非常繁琐,并且不是在每次页面加载时都运行的好主意.如果我匹配整个_controller字符串,那么我的主菜单条目将独立于操作而处于活动状态.

The main navigation entry should be active when we are in the related Controller no matter of the action or any other parameters. And here's the problem. In symfony's config you have _controller: ABBundle:NameController:nameAction, extracting the controller and action name from which is quite heavy, and not a good idea to run on every page load. If I match the whole _controller string then my main menu entries wont be active independant of the action..

我还认为,即使是面包屑也可以由该模式生成,第一个链接是主导航中活动链接的副本,第二个链接是子导航菜单的活动条目,其余可以通过设置根据逻辑对控制器进行控制.

Also I thought even breadcrumbs could be generated by this schema, the first link would be a copy of the active link from the main navigation, the second could be the active entry of the sub navigation menu and the rest could be set via the controller according to logic.

有什么想法吗?

推荐答案

我是这样做的. 我已经将KnpMenu注册为服务,并将entityManager传递给了该类.

I did it this way. I've registered the KnpMenu as a service and passed the entityManager to the class.

stex_site_main.menu_builder:
    class: Stex\SiteBundle\Menu\MenuBuilder
    arguments: ["@knp_menu.factory", "@doctrine.orm.entity_manager"]

stex_site_main.menu.main:
    class: Knp\Menu\MenuItem
    factory_service: stex_site_main.menu_builder
    factory_method: createMainMenu
    arguments: ["@request"]
    scope: request
    tags:
        - { name: knp_menu.menu, alias: main }

我手握EM,打开Entity MyMenu并生成写在数据库中的菜单项.

With EM in hand I open an Entity MyMenu and generate menu entries written in the db.

private $factory;
private $em;
public function __construct(FactoryInterface $factory, \Doctrine\ORM\EntityManager $em)
{
    $this->factory = $factory;
    $this->em = $em;
}
public function createMainMenu(\Symfony\Component\HttpFoundation\Request $request)
{
    $menu = $this->factory->createItem('root');
    $menu->setChildrenAttribute('class', 'menu');
    $menu->addChild(' ', array('route' => 'stex_site_home_home', 'attributes' => array('class' => 'home')));
    $menu->setCurrentUri($request->getRequestUri());
    $em = $this->em;
    $q = $em->createQuery('
        SELECT m
        FROM StexAdminBundle:MyMenu m
        WHERE m.status=1
        ORDER BY menu.position');
    $r = $q->getResult();
    foreach($r as $menu) {
        $menu->addChild($menu->getName(), array('route' => $menu->getRoute(), 'routeParameters' => array(json_decode($menu->getParams())));
    }
    return $menu;
}

将控制器映射到菜单项(和sub sub sub等)在现实世界的应用程序中是非常罕见的情况(根据我的说法),并且不会非常有用(请注意,您必须编写大部分例外",而该控制器不能包括等).

Mapping controllers to menu entries (and sub sub sub etc.) is very rare case in real world app (according to me) and will not be very usable (thought you will have to code mostly 'exceptions' which controller not to include, etc.).

具有DB菜单对我来说是解决方案. 十分灵活,用户可以使用漂亮的界面等对其进行编辑.

Having DB menu is the solution for me. Very flexible, users can edit it with nice interface etc.

这篇关于Symfony 2:自动生成的导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆