KnpMenu provides an extension for the Silex microframework.
The Knp\Menu\Silex\RouterAwareFactory
extends the default factory to add
the support of the url generator of the Symfony2 Routing component. You can
then pass 3 new options to the factory:
route
: The route name (the generator will be used if the name is not null
)routeParameters
: The parameters to generate the url (if omitted, an empty array is used)routeAbsolute
: Whether the generated url should be absolute (default false
)NOTE When you give both a route and an uri to the factory, the route will be used.
<?php
// registering the autoloader for the library.
$app['autoloader']->registerNamespace('Knp\Menu', __DIR__.'/vendor/KnpMenu/src');
// registering the extension
$app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
WARNING The Twig integration is available only when the KnpMenuServiceProvider is registered after the TwigServiceProvider in your application.
alias => id
pair for the
menu provider.alias => id
pair for
the renderer provider.'list'
)Making your menu available through the menu provider is really easy. Simply create the menu as a service in the application and register it in the parameter:
<?php
$app['my_main_menu'] = function($app) {
$menu = $app['knp_menu.factory']->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
// ... add more children
return $menu;
};
$app['knp_menu.menus'] = array('main' => 'my_main_menu');
Your menu is now available in the menu provider
with the name main
.