PHP code example of unlooped / menu-bundle

1. Go to this page and download the library: Download unlooped/menu-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unlooped / menu-bundle example snippets


// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Unlooped\MenuBundle\UnloopedMenuBundle(),
        ];

        // ...
    }

    // ...
}



namespace App\Service;

use Doctrine\Common\Annotations\AnnotationException;
use ReflectionException;
use Unlooped\MenuBundle\Exception\NameForMenuAlreadyExistsException;
use Unlooped\MenuBundle\Exception\ShowAndHideAnnotationSetException;
use Unlooped\MenuBundle\Helper\MenuHelper;
use Unlooped\MenuBundle\Service\AbstractMenuBuilderService;

class MenuBuilderService extends AbstractMenuBuilderService 
{

    /**
     * @throws NameForMenuAlreadyExistsException
     * @throws AnnotationException
     * @throws ReflectionException
     * @throws ShowAndHideAnnotationSetException
     */
    public function createMainMenu(): MenuHelper
    {
        return $this->createMenuHelper()
            ->addMenu('Link A', ['route' => 'route_name_a'])
            ->addMenu('Link B', ['route' => 'route_name_b'])
            ->addMenu('Link C', ['route' => 'route_name_c'])
            ;
    }

}

[
    'label'           => null, // null or string
    'attr'            => [], // array
    'route'           => '', // string
    'routeOptions'    => [], // see route options
    'routeAsAbsolute' => true, // bool
    'url'             => '', // string
    'other'           => [], // array
    'visible'         => true, // bool
]

public function createMainMenu(): MenuHelper
{
    return $this->createMenuHelper()
        ->addSubMenu('Category 1', [options])
            ->addMenu('Link 1 A', ['route' => 'route_name_c1_a'])
            ->addMenu('Link 1 B', ['route' => 'route_name_c1_b'])
            ->addMenu('Link 1 C', ['route' => 'route_name_c1_c'])
        ->end()
        ->addSubMenu('Category 2', [options])
            ->addMenu('Link 2 A', ['route' => 'route_name_c2_a'])
            ->addMenu('Link 2 B', ['route' => 'route_name_c2_b'])
            ->addMenu('Link 2 C', ['route' => 'route_name_c2_c'])
            ->addSubMenu('Category 2 A', ['route' => 'route_name_cat2a'])
                ->addMenu('Link 2A A', ['route' => 'route_name_c2_a'])
                ->addMenu('Link 2A B', ['route' => 'route_name_c2_b'])
                ->addMenu('Link 2A C', ['route' => 'route_name_c2_c'])
            ->end()
        ->end()
        ;
}

/**
 * @Route("/home", name="home")
 *
 * @HideInMenu()
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}

/**
 * @Route("/home", name="home")
 *
 * @HideInMenu(forRoles={"ROLE_GUEST", "ROLE_USER"})
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}

/**
 * @Route("/home", name="home")
 *
 * @ShowInMenu(forRoles={"ROLE_ADMIN", "ROLE_USER"})
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}