我刚刚将SypfenuBundle添加到我的Symfony项目中。 我还添加了Bootstrap以使其看起来更好。
现在我要创建一个子菜单或Dropdownmenu。 有一个简单的例子/方式如何做到这一点? 我搜索了很多例子,但要么需要额外的捆绑,要么不能正常工作
更新
我需要知道如何向菜单子项添加属性以便使用bootstrap呈现它
祝你好运
奥利弗
答案 0 :(得分:0)
这很简单。有关属性,请参阅KnpMenu docs。
一个例子:
base.html.twig
,显示在{% extends "base.html.twig" %}
的模板中:
...
{% block stylesheets %}
<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet" media="all">
<link href="{{ asset('css/pdf.css') }}" rel="stylesheet" media="all">
<link href="{{ asset('css/jquery-ui.min.css') }}" rel="stylesheet" media="all" />
{% endblock %}
...
{% block nav %}
{% spaceless %}
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"></a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
{{ knp_menu_render('AppBundle:Builder:mainMenu', {'style': 'navbar'}) }}
{{ knp_menu_render('AppBundle:Builder:logoutMenu', { 'style': 'navbar-right' }) }}
</div>
</nav>
{% endspaceless %}
{% endblock nav %}
...
{% block javascripts %}
<script
src="https://code.jquery.com/jquery-1.11.3.js"
integrity="sha256-IGWuzKD7mwVnNY01LtXxq3L84Tm/RJtNCYBfXZw3Je0="
crossorigin="anonymous"></script>
<script src="{{ asset('js/bootstrap.js') }}"></script>
<script src="{{ asset('js/jquery-ui.min.js') }}"></script>
<script src="{{ asset('js/project.js') }}"></script>
{% endblock %}
Builder.php
:
...
class Builder implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function mainMenu(FactoryInterface $factory, array $options)
{
$checker = $this->container->get('security.authorization_checker');
$menu = $factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('Receipt');
$menu['Receipt']->addChild('Add receipt', [
'route' => 'receipt_add'
]);
$menu['Receipt']->addChild('Edit receipt', [
'route' => 'receipt_edit'
]);
$menu->addChild('Artist');
$menu['Artist']->addChild('Add artist', [
'route' => 'artist_add'
]);
$menu['Artist']->addChild('Edit artist', [
'route' => 'artist_edit'
]);
$menu['Artist']->addChild('Add existing to show', [
'route' => 'existing_artists'
]);
$menu->addChild('Tickets');
$menu['Tickets']->addChild('Add block', [
'route' => 'block_add'
]);
$menu['Tickets']->addChild('Edit block', [
'route' => 'block_edit'
]);
$menu->addChild('Show');
$menu['Show']->addChild('Add show', [
'route' => 'show_add'
]);
$menu['Show']->addChild('Edit show', [
'route' => 'show_edit'
]);
$menu->addChild('Reports', [
'route' => 'reports'
]);
if ($checker->isGranted('ROLE_ADMIN')) {
$menu->addChild('Admin');
$menu['Admin']->addChild('Ticket block reassign', [
'route' => 'block_reassign'
]);
$menu['Admin']->addChild('Users', [
'route' => 'easyadmin'
]);
$menu['Admin']->addChild('Delete artist', [
'route' => 'artist_delete'
]);
}
return $menu;
}
...