PHP code example of danilovl / menu-builder-bundle
1. Go to this page and download the library: Download danilovl/menu-builder-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/ */
#[Route('/admin/menu', name: 'admin_menu')]
#[IsGranted('ROLE_ADMIN')]
public function admin(): Response
{
return $this->forward('Danilovl\\MenuBuilderBundle\\Controller\\DashboardController::index');
}
use Danilovl\MenuBuilderBundle\Service\MenuManager;
final class SomeService
{
public function __construct(private MenuManager $menus) {}
public function bootstrap(): void
{
$home = $this->menus->create([
'menuName' => 'main',
'label' => 'Home',
'route' => 'app_home',
'icon' => 'fa fa-home',
]);
$this->menus->create([
'menuName' => 'main',
'label' => 'About',
'route' => 'app_about',
'parentId' => $home->getId(),
]);
$this->menus->setMenuActive('main', true);
$tree = $this->menus->getTree('main'); // permission-filtered for current user
}
}
use Danilovl\MenuBuilderBundle\Attribute\MenuItem;
use Symfony\Component\Routing\Attribute\Route;
final class ProfileController
{
#[Route('/profile', name: 'app_profile')]
#[MenuItem(menu: 'main', label: 'Profile', position: 10, icon: 'fa fa-user',
// Custom controller — render the same menu in user's locale
return $this->render('layout.html.twig', [
'locale' => $request->getLocale(),
]);
namespace App\Menu;
use Danilovl\MenuBuilderBundle\Service\UserCatalogInterface;
use Doctrine\ORM\EntityManagerInterface;
final class DoctrineUserCatalog implements UserCatalogInterface
{
public function __construct(private EntityManagerInterface $em) {}
/**
* @return array<int, string>
*/
public function search(string $term, int $limit): array
{
$rows = $this->em->createQueryBuilder()
->select('u.email')
->from(User::class, 'u')
->where('LOWER(u.email) LIKE LOWER(:q)')
->setParameter('q', '%' . $term . '%')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return array_column($rows, 'email');
}
}
use Danilovl\MenuBuilderBundle\Event\MenuItemSavedEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: MenuItemSavedEvent::class)]
public function onSave(MenuItemSavedEvent $event): void
{
$this->logger->info('Menu changed', ['id' => $event->item->getId()]);
}