@@ -14,20 +14,22 @@ following command to download the latest stable version of this bundle:
14
14
15
15
.. code-block :: bash
16
16
17
- $ composer require knplabs/knp-menu-bundle " ^2.0 "
17
+ $ composer require knplabs/knp-menu-bundle
18
18
19
19
This command requires you to have Composer installed globally, as explained
20
20
in the `installation chapter `_ of the Composer documentation.
21
21
22
22
Step 2: Enable the Bundle
23
23
~~~~~~~~~~~~~~~~~~~~~~~~~
24
24
25
- Then, enable the bundle by adding the following line in the ``app/AppKernel.php ``
26
- file of your project:
25
+ KnpMenuBundle should be automatically enabled and configured, thanks to `Flex `_.
26
+
27
+ If you don't use Flex, you can manually enable it, by adding the following line in
28
+ your project's Kernel:
27
29
28
30
.. code-block :: php
29
31
30
- // app/AppKernel.php
32
+ // e.g. app/AppKernel.php
31
33
32
34
// ...
33
35
class AppKernel extends Kernel
@@ -56,7 +58,7 @@ You can define these options if you need to change them:
56
58
57
59
.. code-block :: yaml
58
60
59
- # app/ config/config.yml
61
+ # config/packages/knp_menu.yaml
60
62
knp_menu :
61
63
# use "twig: false" to disable the Twig extension and the TwigRenderer
62
64
twig :
@@ -68,7 +70,7 @@ You can define these options if you need to change them:
68
70
69
71
.. code-block :: xml
70
72
71
- <!-- app/ config/config .xml -->
73
+ <!-- config/packages/knp_menu .xml -->
72
74
<?xml version =" 1.0" charset =" UTF-8" ?>
73
75
<container xmlns =" http://symfony.com/schema/dic/services"
74
76
xmlns : knp-menu =" http://knplabs.com/schema/dic/menu" >
@@ -88,7 +90,7 @@ You can define these options if you need to change them:
88
90
89
91
.. code-block :: php
90
92
91
- // app/ config/config .php
93
+ // config/packages/knp_menu .php
92
94
$container->loadFromExtension('knp_menu', [
93
95
// use 'twig' => false to disable the Twig extension and the TwigRenderer
94
96
'twig' => [
@@ -100,11 +102,6 @@ You can define these options if you need to change them:
100
102
'default_renderer' => 'twig',
101
103
]);
102
104
103
- .. versionadded::2.1.2
104
-
105
- The template used to be ``knp_menu.html.twig`` which did not translate menu entries.
106
- Version 2.1.2 adds the template that translates menu entries.
107
-
108
105
.. note ::
109
106
110
107
Take care to change the default renderer if you disable the Twig support.
@@ -126,18 +123,20 @@ An example builder class would look like this:
126
123
127
124
.. code-block :: php
128
125
129
- // src/AppBundle/ Menu/Builder.php
130
- namespace AppBundle \Menu;
126
+ // src/Menu/Builder.php
127
+ namespace App \Menu;
131
128
129
+ use App\Entity\Blog;
132
130
use Knp\Menu\FactoryInterface;
131
+ use Knp\Menu\ItemInterface;
133
132
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
134
133
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
135
134
136
- class Builder implements ContainerAwareInterface
135
+ final class Builder implements ContainerAwareInterface
137
136
{
138
137
use ContainerAwareTrait;
139
138
140
- public function mainMenu(FactoryInterface $factory, array $options)
139
+ public function mainMenu(FactoryInterface $factory, array $options): ItemInterface
141
140
{
142
141
$menu = $factory->createItem('root');
143
142
@@ -146,7 +145,7 @@ An example builder class would look like this:
146
145
// access services from the container!
147
146
$em = $this->container->get('doctrine')->getManager();
148
147
// findMostRecent and Blog are just imaginary examples
149
- $blog = $em->getRepository('AppBundle: Blog' )->findMostRecent();
148
+ $blog = $em->getRepository(Blog::class )->findMostRecent();
150
149
151
150
$menu->addChild('Latest Blog Post', [
152
151
'route' => 'blog_show',
@@ -199,18 +198,18 @@ To actually render the menu, just do the following from anywhere in any template
199
198
200
199
.. code-block :: html+jinja
201
200
202
- {{ knp_menu_render('AppBundle :Builder: mainMenu') }}
201
+ {{ knp_menu_render('App :Builder: mainMenu') }}
203
202
204
203
.. code-block :: html+php
205
204
206
- <?php echo $view['knp_menu']->render('AppBundle :Builder: mainMenu') ?>
205
+ <?php echo $view['knp_menu']->render('App :Builder: mainMenu') ?>
207
206
208
207
With this method, you refer to the menu using a three-part string:
209
208
**bundle **:**class **:**method **.
210
209
211
210
If you needed to create a second menu, you'd simply add another method to
212
211
the ``Builder `` class (e.g. ``sidebarMenu ``), build and return the new menu,
213
- then render it via ``AppBundle :Builder:sidebarMenu ``.
212
+ then render it via ``App :Builder:sidebarMenu ``.
214
213
215
214
That's it! The menu is *very * configurable. For more details, see the
216
215
`KnpMenu documentation `_.
@@ -242,23 +241,23 @@ way, then do the following:
242
241
243
242
.. code-block :: html+jinja
244
243
245
- {{ knp_menu_render('AppBundle :Builder: mainMenu') }}
244
+ {{ knp_menu_render('App :Builder: mainMenu') }}
246
245
247
246
.. code-block :: html+php
248
247
249
- <?php echo $view['knp_menu']->render('AppBundle :Builder: mainMenu') ?>
248
+ <?php echo $view['knp_menu']->render('App :Builder: mainMenu') ?>
250
249
251
250
Additionally, you can pass some options to the renderer:
252
251
253
252
.. configuration-block ::
254
253
255
254
.. code-block :: html+jinja
256
255
257
- {{ knp_menu_render('AppBundle :Builder: mainMenu', {'depth': 2, 'currentAsLink': false}) }}
256
+ {{ knp_menu_render('App :Builder: mainMenu', {'depth': 2, 'currentAsLink': false}) }}
258
257
259
258
.. code-block :: html+php
260
259
261
- <?php echo $view['knp_menu']->render('AppBundle :Builder: mainMenu', [
260
+ <?php echo $view['knp_menu']->render('App :Builder: mainMenu', [
262
261
'depth' => 2,
263
262
'currentAsLink' => false,
264
263
]) ?>
@@ -272,12 +271,12 @@ You can also "get" a menu, which you can use to render later:
272
271
273
272
.. code-block :: html+jinja
274
273
275
- {% set menuItem = knp_menu_get('AppBundle :Builder: mainMenu') %}
274
+ {% set menuItem = knp_menu_get('App :Builder: mainMenu') %}
276
275
{{ knp_menu_render(menuItem) }}
277
276
278
277
.. code-block :: html+php
279
278
280
- <?php $menuItem = $view['knp_menu']->get('AppBundle :Builder: mainMenu') ?>
279
+ <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu') ?>
281
280
<?php echo $view['knp_menu']->render($menuItem) ?>
282
281
283
282
If you want to only retrieve a certain branch of the menu, you can do the
@@ -288,13 +287,13 @@ beneath it.
288
287
289
288
.. code-block :: html+jinja
290
289
291
- {% set menuItem = knp_menu_get('AppBundle :Builder: mainMenu', ['Contact']) %}
292
- {{ knp_menu_render(['AppBundle :Builder: mainMenu', 'Contact']) }}
290
+ {% set menuItem = knp_menu_get('App :Builder: mainMenu', ['Contact']) %}
291
+ {{ knp_menu_render(['App :Builder: mainMenu', 'Contact']) }}
293
292
294
293
.. code-block :: html+php
295
294
296
- <?php $menuItem = $view['knp_menu']->get('AppBundle :Builder: mainMenu', ['Contact']) ?>
297
- <?php echo $view['knp_menu']->render(['AppBundle :Builder: mainMenu', 'Contact']) ?>
295
+ <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu', ['Contact']) ?>
296
+ <?php echo $view['knp_menu']->render(['App :Builder: mainMenu', 'Contact']) ?>
298
297
299
298
If you want to pass some options to the builder, you can use the third parameter
300
299
of the ``knp_menu_get `` function:
@@ -303,12 +302,12 @@ of the ``knp_menu_get`` function:
303
302
304
303
.. code-block :: html+jinja
305
304
306
- {% set menuItem = knp_menu_get('AppBundle :Builder: mainMenu', [], {'some_option': 'my_value'}) %}
305
+ {% set menuItem = knp_menu_get('App :Builder: mainMenu', [], {'some_option': 'my_value'}) %}
307
306
{{ knp_menu_render(menuItem) }}
308
307
309
308
.. code-block :: html+php
310
309
311
- <?php $menuItem = $view['knp_menu']->get('AppBundle :Builder: mainMenu', [], [
310
+ <?php $menuItem = $view['knp_menu']->get('App :Builder: mainMenu', [], [
312
311
'some_option' => 'my_value'
313
312
]) ?>
314
313
<?php echo $view['knp_menu']->render($menuItem) ?>
@@ -328,4 +327,5 @@ More Advanced Stuff
328
327
disabling_providers
329
328
330
329
.. _`installation chapter` : https://getcomposer.org/doc/00-intro.md
330
+ .. _`Flex` : https://flex.symfony.com
331
331
.. _`KnpMenu documentation` : https://github.com/KnpLabs/KnpMenu/blob/master/doc/01-Basic-Menus.md
0 commit comments