PHP code example of chamber-orchestra / menu-bundle
1. Go to this page and download the library: Download chamber-orchestra/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/ */
namespace App\Navigation;
use ChamberOrchestra\MenuBundle\Menu\MenuBuilder;
use ChamberOrchestra\MenuBundle\Navigation\AbstractCachedNavigation;
use Symfony\Contracts\Cache\ItemInterface;
final class MainNavigation extends AbstractCachedNavigation
{
public function __construct(private readonly string $locale)
{
parent::__construct();
}
// Override the cache key if you need per-locale or per-user trees
public function getCacheKey(): string
{
return 'main_nav_'.$this->locale;
}
// Fine-tune TTL and tags
public function configureCacheItem(ItemInterface $item): void
{
$item->expiresAfter(3600);
$item->tag(['navigation', 'main_nav']);
}
public function build(MenuBuilder $builder, array $options = []): void
{
$builder->add('home', ['label' => 'Home', 'route' => 'app_home']);
}
}
use ChamberOrchestra\MenuBundle\Navigation\ClosureNavigation;
// Uncached (default)
$nav = new ClosureNavigation(function (MenuBuilder $builder): void {
$builder->add('home', ['label' => 'Home', 'route' => 'app_home']);
});
// Cached for 1 hour
$nav = new ClosureNavigation(
callback: function (MenuBuilder $builder): void {
$builder->add('home', ['label' => 'Home', 'route' => 'app_home']);
},
cacheKey: 'sidebar_nav',
ttl: 3600,
);
$builder->add('blog', [
'label' => 'Blog',
'route' => 'app_blog_post_index',
'routes' => [
['route' => 'app_blog_.*'], // all blog_* routes keep the item active
],
]);
namespace App\Navigation\Voter;
use ChamberOrchestra\MenuBundle\Matcher\Voter\VoterInterface;
use ChamberOrchestra\MenuBundle\Menu\Item;
final class QueryParamVoter implements VoterInterface
{
public function __construct(private readonly RequestStack $requestStack)
{
}
public function matchItem(Item $item): ?bool
{
// Return true to mark current, false to reject, null to abstain
$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
return null;
}
$expectedTab = $item->getOption('tab');
if (null === $expectedTab) {
return null;
}
return $request->query->get('tab') === $expectedTab ? true : null;
}
}
namespace App\Navigation\Extension;
use App\Repository\MessageRepository;
use ChamberOrchestra\MenuBundle\Factory\Extension\RuntimeExtensionInterface;
use ChamberOrchestra\MenuBundle\Menu\Item;
final class InboxBadgeExtension implements RuntimeExtensionInterface
{
public function __construct(private readonly MessageRepository $messages)
{
}
public function processItem(Item $item): void
{
if ('inbox' === $item->getName()) {
$item->setExtra('badge', $this->messages->countUnread());
}
}
}
use ChamberOrchestra\MenuBundle\Factory\Extension\ExtensionInterface;
final class TooltipExtension implements ExtensionInterface
{
public function buildOptions(array $options): array
{
if (isset($options['tooltip'])) {
$extras = $options['extras'] ?? [];
$extras['tooltip'] = $options['tooltip'];
$options['extras'] = $extras;
unset($options['tooltip']);
}
return $options;
}
}
use ChamberOrchestra\MenuBundle\Factory\Extension\RuntimeExtensionInterface;
use ChamberOrchestra\MenuBundle\Menu\Item;
final class NotificationBadgeExtension implements RuntimeExtensionInterface
{
public function __construct(private readonly NotificationRepository $notifications) {}
public function processItem(Item $item): void
{
if ('alerts' === $item->getName()) {
$item->setExtra('badge', $this->notifications->countUnread());
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.