knpMenu无法添加动态路线

时间:2017-10-17 02:07:24

标签: symfony knpmenubundle

我正在使用symfony 3.3和knpMenu 2.2 我已将菜单构建器注册为服务

    lilworks_store.menu_builder:
    class: LilWorks\StoreBundle\Menu\MenuBuilder
    arguments: ["@knp_menu.factory","@request_stack"]
    tags:
        - { name: knp_menu.menu_builder, method: createMainMenu, alias: main } # The alias is what is used to retrieve the menu

我可以毫无问题地渲染一个简单的菜单。但是,当我尝试使用参数路由时,我有一个错误

  

在呈现模板期间抛出异常("参数" customer_id"对于路线" customer_show"必须匹配" [^ /] ++ "(""给出)生成相应的URL。")。

我可以在$ this-> requestStack-> getCurrentRequest() - > get(' customer_id')的条件下避免这种情况,仅在路由参数可用时创建子项。

class MenuBuilder
{


private $factory;
private $requestStack;

/**
 * @param FactoryInterface $factory
 *
 * Add any other dependency you need
 */
public function __construct(FactoryInterface $factory,RequestStack $requestStack)
{
    $this->factory = $factory;
    $this->requestStack = $requestStack;
}
public function createMainMenu(array $options)
{

    $menu = $this->factory->createItem('root');

    $menu->addChild('storebundle.menu.storehome', array('route' => 'site_homepage'));
    $menu->addChild('storebundle.menu.adminhome', array('route' => 'lilworks_store_homepage'));
    $userMenuCat = $menu->addChild('storebundle.menu.cat.user');

    $customerMenu = $userMenuCat->addChild('storebundle.menu.customer', array('route' => 'customer_index'));
    $customerMenu->addChild('storebundle.menu.customer.new', array('route' => 'customer_new'));
    if($this->requestStack->getCurrentRequest()->get('customer_id')){
        $customerMenu->addChild('storebundle.menu.customer.show', array('route' => 'customer_show', 'routeParameters' => array('customer_id' => $this->requestStack->getCurrentRequest()->get('customer_id'))));
        $customerMenu->addChild('storebundle.menu.customer.edit', array('route' => 'customer_edit', 'routeParameters' => array('customer_id' => $this->requestStack->getCurrentRequest()->get('customer_id'))));
    }

    $userMenu =  $userMenuCat->addChild('storebundle.menu.user', array('route' => 'user_index'));
    $userMenu->addChild('storebundle.menu.user.new', array('route' => 'user_new'));
    if($this->requestStack->getCurrentRequest()->get('user_id')){
        #$userMenu->addChild('storebundle.menu.user.show', array('route' => 'user_show', 'routeParameters' => array('user_id' => $this->requestStack->getCurrentRequest()->get('user_id'))));
        #$userMenu->addChild('storebundle.menu.user.edit', array('route' => 'user_edit', 'routeParameters' => array('user_id' => $this->requestStack->getCurrentRequest()->get('user_id'))));
    }

    $sessionMenu = $userMenuCat->addChild('storebundle.menu.session', array('route' => 'session_index'));
    if($this->requestStack->getCurrentRequest()->get('session_id')){
        #$sessionMenu->addChild('storebundle.menu.session.show', array('route' => 'session_show', 'routeParameters' => array('session_id' => $this->requestStack->getCurrentRequest()->get('session_id'))));
        #$sessionMenu->addChild('storebundle.menu.session.edit', array('route' => 'session_edit', 'routeParameters' => array('session_id' => $this->requestStack->getCurrentRequest()->get('session_id'))));
    }


    $orderMenuCat = $menu->addChild('storebundle.menu.cat.order');
    $orderMenu = $orderMenuCat->addChild('storebundle.menu.order', array('route' => 'order_index'));
    $orderMenu->addChild('storebundle.menu.order.new', array('route' => 'order_new'));
    if($this->requestStack->getCurrentRequest()->get('order_id')){
        #$orderMenu->addChild('storebundle.menu.order.show', array('route' => 'order_show', 'routeParameters' => array('order_id' => $this->requestStack->getCurrentRequest()->get('order_id'))));
        #$orderMenu->addChild('storebundle.menu.order.edit', array('route' => 'order_edit', 'routeParameters' => array('order_id' => $this->requestStack->getCurrentRequest()->get('order_id'))));
    }

    $couponMenu = $orderMenuCat->addChild('storebundle.menu.coupon', array('route' => 'coupon_index'));
    $couponMenu->addChild('storebundle.menu.coupon.new', array('route' => 'coupon_new'));
    if($this->requestStack->getCurrentRequest()->get('coupon_id')){
        #$couponMenu->addChild('storebundle.menu.coupon.show', array('route' => 'coupon_show', 'routeParameters' => array('coupon_id' => $this->requestStack->getCurrentRequest()->get('coupon_id'))));
        #$couponMenu->addChild('storebundle.menu.coupon.edit', array('route' => 'coupon_edit', 'routeParameters' => array('coupon_id' => $this->requestStack->getCurrentRequest()->get('coupon_id'))));
    }
    $depositSaleMenu = $orderMenuCat->addChild('storebundle.menu.depositsale', array('route' => 'coupon_index'));
    $depositSaleMenu->addChild('storebundle.menu.depositsale.new', array('route' => 'depositSale_new'));
    if($this->requestStack->getCurrentRequest()->get('depositsale_id')){
        #$depositSaleMenu->addChild('storebundle.menu.coupon.show', array('route' => 'depositSale_show', 'routeParameters' => array('depositsale_id' => $this->requestStack->getCurrentRequest()->get('depositsale_id'))));
        #$depositSaleMenu->addChild('storebundle.menu.coupon.edit', array('route' => 'depositSale_edit', 'routeParameters' => array('depositsale_id' => $this->requestStack->getCurrentRequest()->get('depositsale_id'))));
    }
    return $menu;
}

在所有示例中,我看到没有人使用此条件来构建具有参数路由的菜单。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

你没有做错任何事。如果您需要来自请求的信息,例如来自URL的参数或来自服务的用户名(例如$this->tokenStorage->getToken()->getUser();),那么请安排获取并使用它。

在我自己的代码中,我已经提取用户(如上所述 - 假设它存在)用它来建立链接:

    $menu->addChild('menu.edit_settings', [
        'route' => 'ca_profile_update',
        'routeParameters' => array('username' => $user->getUsernameCanonical()),
    ]);

对你而言,我唯一要做的就是将各种参数(' user_id'等等)一次放入变量然后使用它,而不是不断地重新获取它(& #39;在我看来有点过于冗长)。这只是将菜单服务稍微拆分成较小的功能,使其更容易理解 - 但这比原始问题更多的是样式和代码标准。

$this->addCustomerMenu(
    $userMenuCat,
    $this->requestStack->getCurrentRequest()->getInt('customer_id', 0)
);

public function addCustomerMenu(MenuItem $userMenuCat, int $customer_id): void
{
    $customerMenu = $userMenuCat->addChild('storebundle.menu.customer', array('route' => 'customer_index'));
    $customerMenu->addChild('storebundle.menu.customer.new', array('route' => 'customer_new'));
    if ($customer_id){
        $customerMenu->addChild('storebundle.menu.customer.show', array('route' => 'customer_show', 'routeParameters' => array('customer_id' => $customer_id)));
        $customerMenu->addChild('storebundle.menu.customer.edit', array('route' => 'customer_edit', 'routeParameters' => array('customer_id' => $customer_id)));
    }
}