Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 785 Bytes

01a-Advanced-Menu.md

File metadata and controls

36 lines (27 loc) · 785 Bytes

Advanced Menu

You can create a menu easily from a Tree structure (a nested set for example) by making it implement Knp\Menu\NodeInterface. You will then be able to create the menu easily, like in the following example.

<?php

namespace App\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Knp\Menu\Loader\NodeLoader;

class Builder
{
    private $factory;

    public function __construct(FactoryInterface $factory)
    {   
        $this->factory = $factory;
    }   

    public function createMenu(): ItemInterface
    {   
        $loader = new NodeLoader($this->factory);
        $rootNode = /* ... get an object implementing \Knp\Menu\NodeInterface */;
        $menu = $loader->load($rootNode);

        return $menu;
    } 
}