KnpMenuBundle和动态路由参数

时间:2014-06-29 18:30:47

标签: php symfony parameters routes knpmenubundle

我在我的网站上使用knpmenubundle,这是我的问题: 我列出了此路由列出的用户列表:/ admin / users 菜单是这样的:

User (is opened)
    list (is active)
    add new

在列表视图中,项目" 列表"菜单中的活动就像我想要的那样 在添加新视图中,项目" 添加新"菜单中的活动就像我想要的那样

但是,当我想要编辑已经存在的用户时,我无法设置活动项目" 用户"因为te路线有一个动态参数

这是我网站的结构:

在src / dn / AdminBundle / Resources / Config / routing.yml

# Users
### List
dnAdmin_usersList:
    pattern: /users/{page}
    defaults: { _controller: dnAdminBundle:User:list, page: 1 }
    requirements:
        page:  \d*

### Edit
dnAdmin_userEdit:
    pattern: /user/edit/{id}
    defaults: { _controller: dnAdminBundle:User:edit}
    requirements:
        id: \d+
在app / ressources / view / layout / html / twig中

{{ render(controller("dnAdminBundle:Common:leftMenu")) }}
src / dn / AdminBundle / Controller / CommonController中的

public function leftMenuAction()
{
    return $this->render('dnAdminBundle:Common:leftMenu.html.twig');
}

在src / dn / AdminBundle / Resources / view / Common / leftMenu / html.twig

{{ knp_menu_render('leftMenu', {'template':'dnAdminBundle:Menu:knp_menu.html.twig', 'currentClass':'active', 'ancestorClass':'active'}) }}

在src / dn / AdminBundle / Resources / config / services.yml

services:
    dn_admin.menu_builder:
        class: dn\AdminBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory"]

    dn_admin.menu.leftMenu:
        class: Knp\Menu\MenuItem
        factory_service: dn_admin.menu_builder
        factory_method: createLeftMenu
        arguments: ["@request"]
        scope: request
        tags:
            - { name: knp_menu.menu, alias: leftMenu }
src / dn / AdminBundle / Menu / MenuBuilder.php中的

namespace dn \ AdminBundle \ Menu;

使用Knp \ Menu \ FactoryInterface; 使用Symfony \ Component \ HttpFoundation \ Request; 类MenuBuilder {     私人$ factory;

/**
 * @param FactoryInterface $factory
 */
public function __construct(FactoryInterface $factory)
{
    $this->factory = $factory;
}

public function createLeftMenu(Request $request)
{
    // root
    $menu = $this->factory->createItem('root');
    $menu->setChildrenAttributes(array('class' => 'sidebar-menu'));

    // Users
    $l = $menu->addChild('Users', array(
        'uri'    => '#',
    ));
    $l->setAttribute('class', 'treeview');
    $l->setChildrenAttribute('class', 'treeview-menu');
    $l->addChild('Liste', array(
        'route'  => 'dnAdmin_userList',
    ));
    $l->addChild('Edit', array(
        'route'           => 'dnAdmin_userEdit',
        'routeParameters' => $request->get('id'),
    ))
    ->setDisplay(false);
}

$ request-> get(' id')返回空字符串

任何人都知道如何在knpmenubundle中获取当前参数?

由于

2 个答案:

答案 0 :(得分:11)

如果您的菜单构建器扩展了ContainerAware,您应该能够访问请求对象。例如:

class Builder extends ContainerAware {
public function mainMenu(FactoryInterface $factory, array $options) {        

    $request = $this->container->get('request');        

    $menu = $factory->createItem('root');
    $parent = $menu->addChild('Users', array('route' => 'users'));
    $parent->addChild('Edit', array('route' => 'user_edit', 'routeParameters' => array('id' => $request->get('id'))));

    return $menu;
}

您的用户菜单项应获取current_ancestor类

答案 1 :(得分:3)

如果您的菜单构建器类没有扩展ContainerAware(并且,作为服务,则不需要它),您可以传递@request_stack或类型化Symfony\Component\HttpFoundation\RequestStack服务,并获取来自那里的身份证:

$id - $this->requestStack->getCurrentRequest()->get('id');
// -or- 
$request = $this->requestStack->getCurrentRequest();
$id = $request->get('id');