Skip to content

fatguytyson/menu-bundle

Repository files navigation

FGCMenuBundle

The FGCMenuBundle is a simple yet robust menu renderer for Symfony4.

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.

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:

$ composer require fgc/menu-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

2. Enable the Bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

This is only needed if you don't have symfony/flex.

// config/bundles.php

// ...
return [
            // ...
            FGC\MenuBundle\FGCMenuBundle::class => ['all' => true],
        ];

3. Configure the bundle

The bundle comes with a sensible default configuration, which is listed below. You can define these options if you need to change them:

# 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

Add Dynamic Menu Items

Follow the instructions to make an event subscriber and listen for the DiscoverMenuEvent::NAME event.

Here, you can $event->addMenuItem(Menu) on the fly.

Make sure to remember to add group names.

And lastly:

Render the menus in your templates.

{# ... #}
{{ fgc_menu() }}
{# ... #}

This renders:

<li>
    <a href="/">
        Home
    </a>
</li>
<li>
    <a href="/admin/">
        Admin Area
    </a>
</li>    

And can be modified with additional parameters.

{{ fgc_menu("Menu Group Name", "template name", (int)depth) }}

Templating the menu

I have a few templates I have needed already.

  • default
  • bootstrap4
  • sb_admin_2

There are more coming, and can easily be overridden by adding them to your app/Resources directory.

{# 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.

Please feel free to submit issues so it can be improved.

About

A simple yet robust menu generator for Symfony3

Resources

License

Stars

Watchers

Forks

Packages

No packages published