symfony2 knp-menu-bundle 的使用

时间:2022-10-25 16:33:29
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1,安装knp-menu-bundle </span>
php composer.phar require "knplabs/knp-menu-bundle: ~2.0" #composer的安装可以百度
</pre>2,更改app/AppKernel.php<pre name="code" class="html"><?php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
    );
    // ...
}
3,配置 (app/config/config.yml)
knp_menu:
    twig:  # use "twig: false" to disable the Twig extension and the TwigRenderer
        template: knp_menu.html.twig #指定渲染模板;
    templating: false # if true, enables the helper for PHP templates
    default_renderer: twig # The renderer to use, list is also available by default

4,使用

在某一bundle下创建Menu文件夹,在Menu文件夹下创建Builder.php文件

<?php
// src/Acme/DemoBundle/Menu/Builder.php
namespace Acme\DemoBundle\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;

class Builder extends ContainerAware
{
    public function mainMenu(FactoryInterface $factory, array $options)
    {
        $menu = $factory->createItem('root');
        $menu->addChild('Home', array('route' => 'homepage')); #route 对应路由名称,如果不存在会报错
        $menu->addChild('About Me', array(
            'route' => 'page_show',
            'routeParameters' => array('id' => 42)
        ));
        // ... add more children

        return $menu;
    }
	//如果有需要再创建一个Menu可以再加一个方法
	public function sidebarMenu(FactoryInterface $factory, array $options)
	{
	
	}
}

在任何模板中就可以使用如下方法
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu') }}

5.扩展
创建一个服务knp_menu;

<?php
// src/Acme/MainBundle/Menu/MenuBuilder.php
namespace Acme\MainBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;
class MenuBuilder
{
    private $factory;
    /**
     * @param FactoryInterface $factory
     */
    public function __construct(FactoryInterface $factory)
    {
        $this->factory = $factory;
    }


    public function createMainMenu(Request $request)
    {
        $menu = $this->factory->createItem('root');


        $menu->addChild('Home', array('route' => 'homepage'));
        // ... add more children


        return $menu;
    }
}


配置服务
# src/Acme/MainBundle/Resources/config/services.yml
services:
    acme_main.menu_builder:
        class: Acme\MainBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory"]


    acme_main.menu.main:
        class: Knp\Menu\MenuItem # the service definition requires setting the class
        factory_service: acme_main.menu_builder
        factory_method: createMainMenu
        arguments: ["@request"]
        scope: request # needed as we have the request as a dependency here
        tags:
            - { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu



添加子项
public function mainMenu(FactoryInterface $factory, array $options)
{
	$menu = $factory->createItem('root');
	$menu->addChild('Home', array('route' => 'acme_demo_default'));
	$menu->addChild('About Me', array(
		'route' => 'acme_demo_form',
		'routeParameters' => array('id' => 42)
	));
	$menu['About Me']->addChild('wawa');
	// ... add more children
	return $menu;
}


只渲染子类
{% set menuItem = knp_menu_get('AcmeDemoBundle:Builder:mainMenu', ['About Me']) %}
{{ knp_menu_render(menuItem) }}

参考文档: 点击打开链接