Controller autowiring fails with a leading backslash in a route definition

Created on 15 January 2025, 7 days ago

Problem/Motivation

Discovered in 🐛 Error on publish-all API Needs work , controller autowiring fails in some cases. You can autowire controllers as services:

routing.yml:

experience_builder.api.publish_all:
  path: '/xb/api/publish-all'
  defaults:
    _controller: '\Drupal\experience_builder\Controller\ApiPublishAllController'

services.yml:

  Drupal\experience_builder\Controller\ApiPublishAllController: {}

However the leading backslash in the controller definition results in an autowiring failure:

ArgumentCountError: Too few arguments to function Drupal\experience_builder\Controller\ApiPublishAllController::__construct(), 0 passed in xb-dev/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php on line 39 and exactly 4 expected

Steps to reproduce

Proposed resolution

Trim the leading backslash from the controller definition either when setting up routing, or in ControllerResolver, or perhaps in ClassResolver.

Remaining tasks

Decide where the fix should lie. ClassResolver would cover more cases, but is it safe to always trim leading backslashes?

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

routing system

Created by

🇬🇧United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024