How current menu items are matched
masterA menu item is considered "current" if it matches the current request. This state can be:
- Explicitly set: Using
$item->setCurrent(true)or$item->setCurrent(false). - Matched via Voters: Using a
Knp\Menu\Matcher\Matcherwith registered voters.
By default, the current class is added to the <li> of the matching item, and current_ancestor is added to its parents. If currentAsLink is set to false in the renderer options, the current item is rendered as a <span> instead of an <a>.
Available Voters
Knp\Menu\Matcher\Voter\UriVoter: Matches against the item's URI.Knp\Menu\Matcher\Voter\RouteVoter: Matches the_routeattribute of a Symfony Request against the item'sroutesextra.Knp\Menu\Matcher\Voter\RegexVoter: Matches the item's URI using a regular expression.
<?php
use Knp\Menu\Matcher\Matcher;
use Knp\Menu\Matcher\Voter\UriVoter;
use Knp\Menu\MenuFactory;
use Knp\Menu\Renderer\ListRenderer;
$factory = new MenuFactory();
$menu = $factory->createItem('My menu');
// set the current state explicitly
$menu['current_item']->setCurrent(true);
$menu['non_current_item']->setCurrent(false);
// Use the voter
$menu['other_item']->setCurrent(null); // default value for items
$matcher = new Matcher();
$matcher->addVoter(new UriVoter($_SERVER['REQUEST_URI']));
$renderer = new ListRenderer($matcher);<?php
use Knp\Menu\Matcher\Matcher;
use Knp\Menu\Matcher\Voter\UriVoter;
use Knp\Menu\MenuFactory;
use Knp\Menu\Renderer\ListRenderer;
$factory = new MenuFactory();
$menu = $factory->createItem('My menu');
// set the current state explicitly
$menu['current_item']->setCurrent(true);
$menu['non_current_item']->setCurrent(false);
// Use the voter
$menu['other_item']->setCurrent(null); // default value for items
$matcher = new Matcher();
$matcher->addVoter(new UriVoter($_SERVER['REQUEST_URI']));
$renderer = new ListRenderer($matcher);