Integrates KnpMenu to Nette framework

Installs: 4 118

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 4

Forks: 4

Open Issues: 1


dev-master / 1.0.x-dev 2018-05-29 06:20 UTC

This package is not auto-updated.

Last update: 2021-04-30 21:40:42 UTC


SmfMenu is just integration library for KnpMenu.


Easiest way to install the addon is via composer:

composer install trunda/smf-menu

and then register the extension in your config.neon:

    - Smf\Menu\Config\Extension

What is KnpMenu?

The KnpMenu library provides object oriented menus for PHP 5.3. With following API.


use Knp\Menu\MenuFactory;
use Knp\Menu\Renderer\ListRenderer;

$factory = new MenuFactory();
$menu = $factory->createItem('My menu');
$menu->addChild('Home', array('uri' => '/'));
$menu->addChild('Comments', array('uri' => '#comments'));
$menu->addChild('Symfony2', array('uri' => ''));
$menu->addChild('Coming soon');

$renderer = new ListRenderer();
echo $renderer->render($menu);

The above menu would render the following HTML:

  <li class="first">
    <a href="/">Home</a>
  <li class="current">
    <a href="#comments">Comments</a>
    <a href="">Symfony2</a>
  <li class="last">
    <span>Coming soon</span>

This way you can finally avoid writing an ugly template to show the selected item, the first and last items, submenus, ...


Extension adds factory for the menu control, so in presenter is usable like this:

abstract class BasePresenter extends Presenter
    /** @var Menu\Control\Factory */
    private $menuFactory;

    public function injectMenuFactory(Menu\Control\Factory $factory)
        $this->menuFactory = $factory;

    protected function createComponentMenu()
        $menu = $this->menuFactory->createControl();
        $root = $menu->getRoot();

        $root->addChild('catalog', array(
           'label' => 'Katalog',
           'icon'  => 'book'

        $root['catalog']->addChild('categories', array(
            'label' => 'Categories',
            'link'  => 'Category:list',
            'icon'  => 'table',

        $root['catalog']->addChild('new', array(
            'label' => 'Product',
            'link'  => 'Product:list',
            'icon'  => 'gift',
        // ....

        return $menu;
    // ...

and then in your template you can use obligate control macro:

{control menu}


You can register as many renderers as you want. Some default renderers are registered by defaul:

list          => Smf\Menu\Renderer\ListRenderer
bootstrapNav  => Smf\Menu\Renderer\BootstrapNavRenderer

You can use them in template

{control menu:list}
{control menu:bootstrapNav}

Registration of new renderer is as easy as adding new service to your config with tag menuRenderer:

         class: My\Super\Renderer
             menuRenderer: mySuperRenderer

And then you can use it:

{control menu:mySuperRenderer}


Credits goes to original authors of KnpMenu library as to authors of KnpMenuBundle.