symfony 5.1: how to define a controller as a service

I’m a bit of a noob when it comes to Symfony. I am attempting to create a bundle with a controller that accepts a service as a constructor argument; however, I am receiving this error:

The controller for URI "/heartbeat" is not callable: Controller 
"AcmeHeartbeatBundleControllerHeartbeatController" has 
required constructor arguments and does not exist in the container. 
Did you forget to define the controller as a service?

This is the Resources/config/services.xml:

<?xml version="1.0" encoding="utf-8"?>
<container xmlns="" xmlns:xsi=""
        <service id="acme_heartbeat.service.heartbeat_service"
            <argument type="string" id="heartbeat.address"/>
        <service id="acme_heartbeat.controller.heartbeat_controller"
            <argument type="service" id="acme_heartbeat.service.heartbeat_service"/>
            <tag name="controller.service_arguments" />

This is the Resources/config/routes.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns=""
    <route id="heartbeat" path="/heartbeat" controller="AcmeHeartbeatBundleControllerHeartbeatController::heartbeat" methods="GET" />

The main Symfony app’s config/routes.yaml:

    resource: '@AcmeHeartbeatBundle/Resources/config/routes.xml'

The Controller/HeartbeatController.php:



namespace AcmeHeartbeatBundleController;

use AcmeHeartbeatBundleServiceHeartbeatService;
use SymfonyBundleFrameworkBundleControllerAbstractController;

class HeartbeatController extends AbstractController
    public function __construct(HeartbeatService $hbs)


This is the Service/HeartbeatService.php:


namespace AcmeHeartbeatBundleService;

class HeartbeatService

Could someone tell me what I’m doing wrong?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *