How to fix: The controller for URI {url} is not callable: Controller {controller} cannot be fetched from the container because it is private. Did you forget to tag the service with “controller.service_arguments”?

/

You defined a Shopware 6 Controller and want to access it using a public URL. Everything looks great, but this Error gets thrown:

The controller for URI {url} is not callable: Controller {controller} cannot be fetched from the container because it is private. Did you forget to tag the service with “controller.service_arguments”?

– Your best friend: Shopware 6

You probably forgot to set the Controller as public="true" in your Resources/services.xml.

Here’s a simple example of how you should define your Controller in the services.xml:

/custom/plugins/<PluginName>/src/Resources/services.xml

<?xml version="1.0" ?> <container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="<PluginName>\Controller\MyController" public="true"> <call method="setContainer"> <argument type="service" id="service_container"/> </call> </service> </services> </container>

… and this is how your Controller should look:

/custom/plugins/<PluginName>/src/Storefront/Controller/MyController.php

<?php declare(strict_types=1); namespace <PluginName>\Storefront\Controller; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\Routing\Annotation\RouteScope; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Shopware\Storefront\Controller\StorefrontController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; /** * @RouteScope(scopes={"storefront"}) */ class MyController extends StorefrontController { /** * @Route("/my-custom-route/{my_route_param}", name="frontend.my-custom-route", options={"seo"="false"}, methods={"GET"}) * * @param Request $request * @param Context $context * @param SalesChannelContext $salesChannelContext * * @return string * @throws \Exception */ public function myCustomRoute(Request $request, Context $context, SalesChannelContext $salesChannelContext) { $param= $request->get('my_route_param'); if (empty($param)) { return $this->redirect('/'); } // Do what you want to do return $this->redirectToRoute('frontend.checkout.register.page'); }

… and the last step is to import the Controller in the routes.xml:

/custom/plugins/<PluginName>/src/Resources/routes.xml

<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/routing" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> <import resource="../../**/Controller/*Controller.php" type="annotation"/> </routes>

This code will import all files that reside in a folder named “Controller” and have a Suffix of *Controller.php.

Hope this get’s you back on track, happy working!

Leave a Comment