@@ -113,145 +113,54 @@ Create your first menu!
113113There are two ways to create a menu: the "easy" way, and the more flexible
114114method of creating a menu as a service.
115115
116- Method a) The Easy Way (yay)!
117- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118-
119- To create a menu, first create a new class in the ``Menu `` directory of one
120- of your bundles. This class - called ``Builder `` in our example - will have
121- one method for each menu that you need to build.
122-
123- An example builder class would look like this:
124-
125- .. code-block :: php
126-
127- // src/Menu/Builder.php
128- namespace App\Menu;
129-
130- use App\Entity\Blog;
131- use Knp\Menu\FactoryInterface;
132- use Knp\Menu\ItemInterface;
133-
134- final class Builder
135- {
136- public function __construct(
137- private EntityManagerInterface $em,
138- ) {
139- }
140-
141- public function mainMenu(FactoryInterface $factory, array $options): ItemInterface
142- {
143- $menu = $factory->createItem('root');
144-
145- $menu->addChild('Home', ['route' => 'homepage']);
146-
147- // findMostRecent and Blog are just imaginary examples
148- $blog = $this->em->getRepository(Blog::class)->findMostRecent();
149-
150- $menu->addChild('Latest Blog Post', [
151- 'route' => 'blog_show',
152- 'routeParameters' => ['id' => $blog->getId()]
153- ]);
154-
155- // create another menu item
156- $menu->addChild('About Me', ['route' => 'about']);
157- // you can also add sub levels to your menus as follows
158- $menu['About Me']->addChild('Edit profile', ['route' => 'edit_profile']);
159-
160- // ... add more children
161-
162- return $menu;
163- }
164- }
165-
166- With the standard ``knp_menu.html.twig `` template and your current page being
167- 'Home', your menu would render with the following markup:
168-
169- .. code-block :: html
170-
171- <ul >
172- <li class =" current first" >
173- <a href =" #route_to/homepage" >Home</a >
174- </li >
175- <li class =" current_ancestor" >
176- <a href =" #route_to/page_show/?id=42" >About Me</a >
177- <ul class =" menu_level_1" >
178- <li class =" current first last" >
179- <a href =" #route_to/edit_profile" >Edit profile</a >
180- </li >
181- </ul >
182- </li >
183- </ul >
184-
185- .. note ::
186-
187- The menu builder can be overwritten using the bundle inheritance.
188-
189- To actually render the menu, just do the following from anywhere in any template:
190-
191- .. configuration-block ::
192-
193- .. code-block :: html+jinja
194-
195- {{ knp_menu_render('App:Builder: mainMenu') }}
196-
197- .. code-block :: html+php
198-
199- <?php echo $view['knp_menu']->render('App:Builder: mainMenu') ?>
200-
201- With this method, you refer to the menu using a three-part string:
202- **bundle **:**class **:**method **.
203-
204- If you needed to create a second menu, you'd simply add another method to
205- the ``Builder `` class (e.g. ``sidebarMenu ``), build and return the new menu,
206- then render it via ``App:Builder:sidebarMenu ``.
207-
208- That's it! The menu is *very * configurable. For more details, see the
209- `KnpMenu documentation `_.
210-
211- Method b) A menu builder as a service
116+ Method a) A menu builder as a service
212117~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213118
214119For information on how to register a menu builder as a service, read
215120:doc: `Creating Menu Builders as Services <menu_builder_service >`.
216121
217-
218- Method c) A menu as a service
122+ Method b) A menu as a service
219123~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220124
221125For information on how to register a service and tag it as a menu, read
222126:doc: `Creating Menus as Services <menu_service >`.
223127
128+ Method c) A menu discovered by convention
129+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130+
131+ For information on how to use menu based on the bundle alias convention,
132+ read :doc: `Creating Menu via Naming Convention <menu_convention >`.
133+
224134.. note ::
225135
226136 To improve performances, you can :doc: `disable providers you don't need <disabling_providers >`.
227137
228138Rendering Menus
229139---------------
230140
231- Once you've set up your menu, rendering it is easy. If you've used the "easy"
232- way, then do the following:
141+ Once you've set up your menu, rendering it is easy.
233142
234143.. configuration-block ::
235144
236145 .. code-block :: html+jinja
237146
238- {{ knp_menu_render('App :Builder: mainMenu ') }}
147+ {{ knp_menu_render('my_main_menu ') }}
239148
240149 .. code-block :: html+php
241150
242- <?php echo $view['knp_menu']->render('App :Builder: mainMenu ') ?>
151+ <?php echo $view['knp_menu']->render('my_main_menu ') ?>
243152
244153Additionally, you can pass some options to the renderer:
245154
246155.. configuration-block ::
247156
248157 .. code-block :: html+jinja
249158
250- {{ knp_menu_render('App :Builder: mainMenu ', {'depth': 2, 'currentAsLink': false}) }}
159+ {{ knp_menu_render('my_main_menu ', {'depth': 2, 'currentAsLink': false}) }}
251160
252161 .. code-block :: html+php
253162
254- <?php echo $view['knp_menu']->render('App :Builder: mainMenu ', [
163+ <?php echo $view['knp_menu']->render('my_main_menu ', [
255164 'depth' => 2,
256165 'currentAsLink' => false,
257166 ]) ?>
@@ -265,12 +174,12 @@ You can also "get" a menu, which you can use to render later:
265174
266175 .. code-block :: html+jinja
267176
268- {% set menuItem = knp_menu_get('App :Builder: mainMenu ') %}
177+ {% set menuItem = knp_menu_get('my_main_menu ') %}
269178 {{ knp_menu_render(menuItem) }}
270179
271180 .. code-block :: html+php
272181
273- <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu ') ?>
182+ <?php $menuItem = $view['knp_menu']->get('my_main_menu ') ?>
274183 <?php echo $view['knp_menu']->render($menuItem) ?>
275184
276185If you want to only retrieve a certain branch of the menu, you can do the
@@ -281,13 +190,13 @@ beneath it.
281190
282191 .. code-block :: html+jinja
283192
284- {% set menuItem = knp_menu_get('App :Builder: mainMenu ', ['Contact']) %}
285- {{ knp_menu_render(['App :Builder: mainMenu ', 'Contact']) }}
193+ {% set menuItem = knp_menu_get('my_main_menu ', ['Contact']) %}
194+ {{ knp_menu_render(['my_main_menu ', 'Contact']) }}
286195
287196 .. code-block :: html+php
288197
289- <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu ', ['Contact']) ?>
290- <?php echo $view['knp_menu']->render(['App :Builder: mainMenu ', 'Contact']) ?>
198+ <?php $menuItem = $view['knp_menu']->get('my_main_menu ', ['Contact']) ?>
199+ <?php echo $view['knp_menu']->render(['my_main_menu ', 'Contact']) ?>
291200
292201If you want to pass some options to the builder, you can use the third parameter
293202of the ``knp_menu_get `` function:
@@ -296,12 +205,12 @@ of the ``knp_menu_get`` function:
296205
297206 .. code-block :: html+jinja
298207
299- {% set menuItem = knp_menu_get('App :Builder: mainMenu ', [], {'some_option': 'my_value'}) %}
208+ {% set menuItem = knp_menu_get('my_main_menu ', [], {'some_option': 'my_value'}) %}
300209 {{ knp_menu_render(menuItem) }}
301210
302211 .. code-block :: html+php
303212
304- <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu ', [], [
213+ <?php $menuItem = $view['knp_menu']->get('my_main_menu ', [], [
305214 'some_option' => 'my_value'
306215 ]) ?>
307216 <?php echo $view['knp_menu']->render($menuItem) ?>
@@ -314,6 +223,7 @@ More Advanced Stuff
314223
315224 menu_service
316225 menu_builder_service
226+ menu_convention
317227 i18n
318228 events
319229 custom_renderer
0 commit comments