baconmanager / menu-bundle
这个包是 symfony2 的一个组件
v0.1
2016-01-03 19:41 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-14 19:03:54 UTC
README
此组件负责自定义 KnpMenuBundle 的菜单创建
安装
要安装此组件,只需运行以下命令:
$ composer require baconmanager/menu-bundle
现在请将以下组件添加到 AppKernel.php 文件中:
<?php // app/AppKernel.php public function registerBundles() { // ... new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Bacon\Bundle\MenuBundle\BaconMenuBundle(), // ... }
配置
请将以下配置添加到 app/config/config.yml 文件中:
#Menu knp_menu: # use "twig: false" to disable the Twig extension and the TwigRenderer twig: template: BaconCoreBundle:partial:menu.html.twig # if true, enables the helper for PHP templates templating: false # the renderer to use, list is also available by default default_renderer: twig
创建菜单
要创建一个新菜单,只需在 MyBundle/Menu/Builder 命名空间中创建一个类,如下例所示:
对于 symfony < 2.7 版本
<?php // src/AppBundle/Menu/Builder.php namespace AppBundle\Menu; use Knp\Menu\FactoryInterface; use Symfony\Component\DependencyInjection\ContainerAware; class Builder extends ContainerAware { public function addMenu(FactoryInterface $factory, array $options) { $menu = $factory->createItem('root'); $translate = $this->container->get('translator'); // Menu Catalog $menu->addChild($translate->trans('Category'))->setAttribute('icon', '<i class="fa fa-book"></i>'); $menu[$translate->trans('Category')]->addChild($translate->trans('List'),array('route' => 'admin_category')); $menu[$translate->trans('Category')]->addChild($translate->trans('New'),array('route' => 'admin_category_new')); return $menu; } }
对于 symfony >= 2.8 版本
<?php // src/AppBundle/Menu/Builder.php namespace AppBundle\Menu; use Knp\Menu\FactoryInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; class Builder implements ContainerAwareInterface { use ContainerAwareTrait; public function addMenu(FactoryInterface $factory, array $options) { $menu = $factory->createItem('root'); $translate = $this->container->get('translator'); // Menu Catalog $menu->addChild($translate->trans('Category'))->setAttribute('icon', '<i class="fa fa-book"></i>'); $menu[$translate->trans('Category')]->addChild($translate->trans('List'),array('route' => 'admin_category')); $menu[$translate->trans('Category')]->addChild($translate->trans('New'),array('route' => 'admin_category_new')); return $menu; } }
在布局(twig)中渲染菜单
{{ bacon_menu_full_render() }}