positibe / menu-bundle
Symfony PositibeMenuBundle
1.0.1
2017-12-17 18:41 UTC
Requires
- doctrine/orm: ~2.5
- gedmo/doctrine-extensions: ~2.4
- knplabs/knp-menu-bundle: ~2.0
- positibe/publishable: ~0.1
- symfony-cmf/core-bundle: ^2.0
- symfony/framework-bundle: ~2.8|~3.0
- twig/twig: ^1.0||^2.0
This package is not auto-updated.
Last update: 2024-09-29 03:04:28 UTC
README
此扩展包提供了一个ORM提供者,用于使用从数据库加载的菜单与KnpMenuBundle一起使用,并受Symfony-Cmf MenuBundle的启发。
安装
要安装此扩展包,只需添加依赖扩展包。
php composer.phar require positibe/orm-menu-bundle
接下来,确保在您的应用程序内核中启用这些扩展包。
<?php
// app/AppKernel.php
public function registerBundles()
{
return array(
// Dependency (check if you already have this bundle included)
new Symfony\Cmf\Bundle\CoreBundle\CmfCoreBundle(),
// Vendor specifics bundles
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Positibe\Bundle\MenuBundle\PositibeMenuBundle(),
// ...
);
}
配置
将配置复制到您的配置包中。
# config/packages/positibe_menu.yaml
parameters:
# locales: [es, en, fr] # Maybe you already have it configured
positibe.menu_node.class: Positibe\Bundle\MenuBundle\Doctrine\Orm\MenuNode
knp_menu:
providers:
builder_alias: false
container_aware: false
twig: # use "twig: false" to disable the Twig extension and the TwigRenderer
template: PositibeMenuBundle::_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
doctrine:
orm:
resolve_target_entities:
Positibe\Bundle\MenuBundle\Model\MenuNodeInterface: "%positibe.menu_node.class%"
注意:此扩展包使用GedmoDoctrineExtension的timestampable、sluggable、softdeletable、translatable和sortable扩展。请确保您已经启用了其监听器。您还可以使用StofDoctrineExtensionBundle。
请记得更新架构。
php app/console doctrine:schema:update --force
使用链接类型菜单
[php]
<?php
$menuClass = $this->container->getParameter('positibe.menu_node.class');
// Creating the root menu that is a container for submenus
$menu = new $menuClass('footer');
$menu->setChildrenAttributes(['class' => 'nav navbar-nav']); //You can set the ul attributes here
$manager->persist($menu);
//Creating an URI menu, that link to a external or internal full url.
/** @var \Positibe\Bundle\MenuBundle\Doctrine\Orm\MenuNode $menuExternalUrl */
$menuExternalUrl = new $menuClass('Github');
$menuExternalUrl->setLinkUri('https://github.com/Positibe/MenuBundle');
$menu->addChild($menuExternalUrl);
$manager->persist($menuExternalUrl); //The menu is configured with cascade persist, so you don't need to do this
// Creating a route menu, that link to a route in the routing configuration of your application
/** @var \Positibe\Bundle\MenuBundle\Doctrine\Orm\MenuNode $menuHomePage */
$menuHomePage = new $menuClass();
$menuHomePage->setName('homepage'); //You can define a code name to have better control of the menus
$menuHomePage->setLabel('Inicio'); //And you can define a proper label to show in the views
$menuHomePage->setLinkRoute('homepage');
$menu->addChild($menuHomePage);
$manager->persist($menuHomePage); //The menu is configured with cascade persist, so you don't need to do this
$manager->flush();
翻译菜单标签
[php]
<?php
$menuClass = $this->container->getParameter('positibe.menu_node.class');
$menuContact = $manager->getRepository($menuClass)->findOneBy(['name' => 'homepage']);
$menuContact->setLabel('Inicio'); //Change the label normally
$menuContact->setLocale('es'); //Then set the proper locale
$manager->persist($menuContact);
$manager->flush();
渲染菜单
您只需要在您的twig模板中使用knp_menu_render
函数。
{% app/Resources/views/base.html.twig %}
{{ knp_menu_render('footer') }}
将实体放入菜单中
您还可以通过实现具有路由的实体来将此扩展包与SymfonyCMf RoutingBundle集成。
有关更多信息,请参阅 Symfony Cmf MenuBundle文档 和 KnpMenuBundle文档