Dependency injection in frontend controller in 5.7.13

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

Hey @marko.richter

make sure that your controller is registered correctly. Have a look here in the documentation: Shopware 5 upgrade guide

Best regards from Schöppingen
Michael Telgmann

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.

    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.

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 :slightly_smiling_face:

1 „Gefällt mir“