JarsOfJam-Scheduler
JarsOfJam-Scheduler

Reputation: 3149

I've defined a bundle Controller and Routing, but I get "Error: the controller does neither exist as service nor as class"

I have created a controller class under a module directory in Symfony 4. My bundle's routing file references it. My root's routing file includes the bundle's routing file.

However I get an error 500, "Error: the controller does neither exist as service nor as class".

Why?

root/src/MyBundle/Resources/config/routing.YML

route_name:
  path: /test3
  controller: MyBundle\Controller\ExportCsvController::exportProductInCsv
  options:
    expose: true

root/config/routes/my_custom_routes.YML

route_name:
      resource: "@MyBundle/Resources/config/routing.yml"
      prefix:   /

root/src/MyBundle/Controller/ExportCsvController.PHP

<?php
namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ExportCsvController extends Controller
{
    public function exportProductInCsv(): Response
    {
        return new Response(
            '<html><body>test</body></html>'
        );
    }
}

Upvotes: 4

Views: 4781

Answers (2)

Nuryagdy Mustapayev
Nuryagdy Mustapayev

Reputation: 775

I faced the same error:

The controller for URI "/api/user-info" is not callable: Controller "App\Controller\Api\UserInfoController" does neither exist as service nor as class.

with the code I got from a colleague. After trying many things I found the issue:

class UserInfoController extends AbstractController
{
    protected function __construct(
        #[Autowire(param: "kernel.project_dir")]
        private string $rootDir,
    ) {
    }

    #[Route('/api/user-info', name: 'api_get_user_info', methods: 'GET')]
    public function getUserInfo(UserInterface $user): JsonResponse
    {
    }
}

Protected constructor!

Error does not appear when you run bin/console debug:container because this class is ignored and the controller is not is not registered as a service.

Error is given only when I visited the route.

Upvotes: 0

JarsOfJam-Scheduler
JarsOfJam-Scheduler

Reputation: 3149

It was a problem of cache (Symfony cache). Solved with this command: php bin/console cache:clear --env=prod. Now the content of the file root/src/MyBundle/Resources/config/routing.YML is correctly taken into account :).

Upvotes: 4

Related Questions