The FGCMenuBundle is a simple yet robust menu renderer for Symfony4., (*1)
The biggest change is the decision to remove Annotation Support. The feature made code handling difficult to manage, and
chasing down menu items difficult. So centralising menu creation in configurations, and events., (*2)
Documentation
Installation
1. Download the bundle
Open a command console, enter your project directory and execute the following command to download the latest
stable version of this bundle:, (*3)
$ composer require fgc/menu-bundle
This command requires you to have Composer installed globally, as explained in the
installation chapter of the Composer documentation., (*4)
2. Enable the Bundle
Then, enable the bundle by adding the following line in the app/AppKernel.php
file of your project:, (*5)
This is only needed if you don't have symfony/flex., (*6)
// config/bundles.php
// ...
return [
// ...
FGC\MenuBundle\FGCMenuBundle::class => ['all' => true],
];
The bundle comes with a sensible default configuration, which is listed below. You can define these options
if you need to change them:, (*7)
# config/packages/fgc_menu.yaml
fgc_menu:
# Menu group name and identifier
# default is also the group given if omitted
default:
# Menu Title
Home Page:
# each option below is optional
# route name for the link to generate
route: homepage
# route parameters if any. If omitted, an empty array is returned
routeOptions:
option: value
option2: value
# icon to be attached to the menu item. Great for dashboards
icon: dashboard
# order of the items, so inserted Items during events can be integrated smoothly
order: 1
# a single ROLE to show only if is_granted() or none to always show This was previously ROLE, and is a
# breaking change.
granted: IS_ANONYMOUS
# This is needed if the granted action needs an object to check against. You may need to dynamically add
# the menu item to add objects rather than strings
grantedObject: 'User'
# to make mult-level menus, menu name to place under this item.
children: user
Follow the instructions to make
an event subscriber and listen for the DiscoverMenuEvent::NAME
event., (*8)
Here, you can $event->addMenuItem(Menu)
on the fly., (*9)
Make sure to remember to add group names., (*10)
And lastly:, (*11)
{# ... #}
{{ fgc_menu() }}
{# ... #}
This renders:, (*12)
<li>
<a href="/">
Home
</a>
</li>
<li>
<a href="/admin/">
Admin Area
</a>
</li>
And can be modified with additional parameters., (*13)
{{ fgc_menu("Menu Group Name", "template name", (int)depth) }}
I have a few templates I have needed already.
* default
* bootstrap4
* sb_admin_2, (*14)
There are more coming, and can easily be overridden by adding them to your app/Resources directory., (*15)
{# templates/bundles/FGCMenuBundle/{template_name}.html.twig #}
{% for item in menu %}
{% if not item.granted or is_granted(item.granted, item.grantedObject) %}
<li>
<a href="{{ item.route ? path(item.route, item.routeOptions) : '#' }}">
{% if item.icon %}<i class="fa fa-{{ item.icon }}" ></i>{% endif %}
{{ item.name }}
</a>
{% if item.children and depth %}
<ul>
{{ fgc_menu(item.children, template, depth) }}
</ul>
{% endif %}
</li>
{% endif %}
{% endfor %}
Afterword
In v2, cache was attempted to be added, and while it saved the cache correctly, there are too many variables to chase
down into to generate the cache key. I may try again, but not now., (*16)
Please feel free to submit issues so it can be improved., (*17)