| Q | A
| ---------------- | -----
| Bug report? | yes
| Feature request? | no
| BC Break report? | no
| RFC? | no
| Sylius version | 1.1.5
I'm getting autowiring error when trying to extend ResourceController.
Exact steps to reproduce:
src/AppBundle/Controller/ProductController.php with following content:<?php
namespace AppBundle\Controller;
use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sylius\Component\Resource\ResourceActions;
class ProductController extends ResourceController
{
public function showAction(Request $request): Response
{
return new JsonResponse(['mesage' => 'test']);
}
}
app/config/config.yml:sylius_product:
resources:
product:
classes:
controller: AppBundle\Controller\ProductController
bin/console debug:router I'm getting following error:Cannot autowire service "AppBundle\Controller\ProductController": argument "$metadata" of method "Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct()" references interface "Sylius\Component\Resource\Metadata\MetadataInterface" but no such service exists. Did you create a class that implements this interface?
Taking in account that I only followed steps from official docs, I guess this is not the desired behavior.
Similar issue was already reported here. The proposed solution did make the mentioned error disappear, but then after setting up a test route for this controller in app/config/routing.yml like this:
app_product_show:
path: /test
methods: [GET]
defaults:
_controller: AppBundle:Product:showAction
and making a GET request on localhost:8000/test I'm getting following error:
Type error: Too few arguments to function Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct(), 0 passed in .../var/cache/dev/ContainerBdinpch/getProductControllerService.php on line 14 and exactly 17 expected
Hello @loffler and welcome to the community!
I think that autowire should indeed be set to false (it should be probably specified in docs), as in mentioned solution. Then, as you use controller of one of the resource, you should use it's service declaration not a class name:
_controller: sylius.controller.product:showAction.
By the way - Sylius issues should be used to report bugs, propose new features, discuss new functionalities. For everything else, please, use our Slack or Forum (preffered) or stackoverflow. Good luck :)
@Zales0123 thanks a lot! Indeed not using service declaration was the problem.
I suspected that the error might be on my side, but I did choose to report it here because at least I considered that the docs are not suffient enough.
Any further hints available for this topic? Running into the same issue. Autowire: false doesnt help as it raises errors that construct will be called with zero arguments instead of expected 17 args. (I have also asked on StackOverflow for this)
Most helpful comment
@Zales0123 thanks a lot! Indeed not using service declaration was the problem.
I suspected that the error might be on my side, but I did choose to report it here because at least I considered that the docs are not suffient enough.