# Dependency injection in frontend controller in 5.7.13

**URL:** <https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344>\
**Category:** Programmierung\
**Tags:** programming\
**Created:** [18. Juli 2022 um 14:45 UTC](https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344 "2022-07-18T14:45:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marko.richter](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@marko.richter](https://forum.shopware.com/u/marko.richter)\
**Post date:** [18. Juli 2022 um 14:45 UTC](https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344/1 "2022-07-18T14:45:16Z")

</div>

Hey,

i am trying to add some services to a frontend controller in shopware 5.7.13.

Since i am not allowed to use the old  
$this-\>container-\>get(‚…‘) // get by identifier  
i added a construct:

> ```
> /**
> */
> public function __construct(
> .....
> )
> {
> .....
> parent::__construct();
> }
> 
> ```

But than the inject is not happening and the  
"engine/Library/Enlight/Controller/Dispatcher/Default.php "  
has missing arguments.

„SwagDigitalPublishing“ controller for example is still using $this-\>container-\>get(‚…‘)  
and is not throwing errors.

Can someone tell me, how this is suppose to be done, the right way, now? Even the controllers in the docs are still using „$this-\>container-\>get(‚…‘)“

thank you

---

<div class="post-metadata">

**Author:** ![Michael\_Telgmann](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/michael_telgmann/32/20289_2.png) [@Michael\_Telgmann](https://forum.shopware.com/u/Michael_Telgmann)\
**Post date:** [19. Juli 2022 um 08:00 UTC](https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344/2 "2022-07-19T08:00:35Z")

</div>

Hey @marko.richter

make sure that your controller is registered correctly. Have a look here in the documentation: [Shopware 5 upgrade guide](https://developers.shopware.com/developers-guide/shopware-5-upgrade-guide-for-developers/#controller-registration-using-di-tag)

Best regards from Schöppingen  
Michael Telgmann

---

<div class="post-metadata">

**Author:** ![marko.richter](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@marko.richter](https://forum.shopware.com/u/marko.richter)\
**Post date:** [19. Juli 2022 um 10:45 UTC](https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344/3 "2022-07-19T10:45:37Z")

</div>

Ok, the Problem was this old Projekt i had to update was done dirty …

They implemented a subscriber that routed to a controller class without namespace.  
I have seen some versions of that in core files but this was in a plugin.

```auto
    public static function getSubscribedEvents()
    {
        return array(
            'Enlight_Controller_Dispatcher_ControllerPath_Frontend_OldClass' => 'onGetControllerPathFrontend',
            );
    }

    /**
     * Register the frontend controller
     *
     * @param \Enlight_Event_EventArgs $args
     * @return string
     * @Enlight\Event Enlight_Controller_Dispatcher_ControllerPath_Frontend_OldClass */
    public function onGetControllerPathFrontend(\Enlight_Event_EventArgs $args)
    {
        return __DIR__. '/../Controllers/Frontend/OldClass.php';
    }

```

There was no service node for the controller and so it was not possible to implement dependency …

i delete that nonsense and implemented the controller in a normal fashion again and now it works.  
Sadly i am not experienced enough to not have seen it from the start and got hung up on the dependency inject and not the baseline. (Even so found it confusing from the start, how this controller lived in that plugin)

Thank you for the doc entry.

---

<div class="post-metadata">

**Author:** ![Michael\_Telgmann](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/michael_telgmann/32/20289_2.png) [@Michael\_Telgmann](https://forum.shopware.com/u/Michael_Telgmann)\
**Post date:** [19. Juli 2022 um 11:12 UTC](https://forum.shopware.com/t/dependency-injection-in-frontend-controller-in-5-7-13/95344/4 "2022-07-19T11:12:52Z")

</div>

Registering controllers via the services.xml definition is only possible since Shopware 5.6.0.  
So the code which you had there before, was the way to go for older SW versions. And of course this still works, but you cannot use DI with this 🙂
