@@ -130,23 +130,22 @@ An example builder class would look like this:
130
130
use App\Entity\Blog;
131
131
use Knp\Menu\FactoryInterface;
132
132
use Knp\Menu\ItemInterface;
133
- use Symfony\Component\DependencyInjection\ContainerAwareInterface;
134
- use Symfony\Component\DependencyInjection\ContainerAwareTrait;
135
133
136
- final class Builder implements ContainerAwareInterface
134
+ final class Builder
137
135
{
138
- use ContainerAwareTrait;
136
+ public function __construct(
137
+ private EntityManagerInterface $em,
138
+ ) {
139
+ }
139
140
140
141
public function mainMenu(FactoryInterface $factory, array $options): ItemInterface
141
142
{
142
143
$menu = $factory->createItem('root');
143
144
144
145
$menu->addChild('Home', ['route' => 'homepage']);
145
146
146
- // access services from the container!
147
- $em = $this->container->get('doctrine')->getManager();
148
147
// findMostRecent and Blog are just imaginary examples
149
- $blog = $em->getRepository(Blog::class)->findMostRecent();
148
+ $blog = $this-> em->getRepository(Blog::class)->findMostRecent();
150
149
151
150
$menu->addChild('Latest Blog Post', [
152
151
'route' => 'blog_show',
@@ -183,12 +182,6 @@ With the standard ``knp_menu.html.twig`` template and your current page being
183
182
</li >
184
183
</ul >
185
184
186
- .. note ::
187
-
188
- You only need to implement ``ContainerAwareInterface `` if you need the
189
- service container. The more elegant way to handle your dependencies is to
190
- inject them in the constructor. If you want to do that, see the method below.
191
-
192
185
.. note ::
193
186
194
187
The menu builder can be overwritten using the bundle inheritance.
0 commit comments