Home > Back-end >  Controller not being registered as a service correctly?
Controller not being registered as a service correctly?

Time:12-01

I am trying to create a basic EntryController controller with admin route in my Sylius/Symfony 5 setup.

My src/Controller/EntryController.php looks as follows:

<?php

namespace App\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class EntryController extends AbstractController
{

    /**
     * @param Request $request
     * @return Response
     */
    public function indexAction(Request $request): Response
    {
       dd('THIS CONTROLLER IS WORKING!');
    }

}

The route for my controller src/Resources/config/routing/admin/order_form.yml looks like the below:

sylius_complete_order_form:
  path: /order/form
  methods: [GET]
  controller: App\Controller\EntryController::index

And my controller is defined as a service inside config/services.yaml:

# Controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
    resource: '../src/Controller'
    public: true
    autowire: true
    tags: ['controller.service_arguments']

Currently when I try navigate to the path of the sylius_complete_order_form route defined above, I am getting the below error:

"App\Controller\EntryController" has no container set, did you forget to define it as a service subscriber?

  • I have tried manually clearing the cache by deleting var/cache
    folder.

  • I have tried running php bin/console cache:clear

  • When I run php bin/console debug:container EntryController the output is the below:

       Service ID       App\Controller\EntryController
       Class            App\Controller\EntryController
       Tags             controller.service_arguments
       Calls            setContainer
       Public           yes
       Synthetic        no
       Lazy             no
       Shared           yes
       Abstract         no
       Autowired        yes
       Autoconfigured   no
    

I don't understand why this is happening?

Any help or guidance would be greatly appreciated. Let me know should I need to include additional info.

CodePudding user response:

I have managed to get my controller working by doing a few steps:

  1. Remove the service definition of the EntryController from services.yaml
  2. Extend AbstractController

class EntryController extends AbstractController

  1. Enable autowire & make the controller public inside my services.yaml config/services.yaml

    App\Controller: resource: '../src/Controller' public: true autowire: true tags: ['controller.service_arguments']

CodePudding user response:

Your class has to extends "AbstractController" from "use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;"

You can check on the docs:

https://symfony.com/doc/current/controller.html#the-base-controller-class-services

(Usually, the default configuration is sufficient for your problem. You shouldn't need to touch it.)

By the way, if you change the conf, don't forget to clear the cache,just to be sure.

Tell me if it's working!

  • Related