Make controllers invokable for DX and maintainability

Created on 27 October 2022, about 2 years ago
Updated 29 November 2024, 20 days ago

Problem/Motivation

Consider updating controllers with a single routable and consistently named method. Each controller would then represent a single route. This would helps prevent bloat in controllers, wherein dependencies and functionality are brought in but not utilized by all routes. It avoids the need to invent a new method name for each controller/route, with a common / standardized entry point. In the case where a route needs to be deprecated or moved, we can easily deprecate an entire class without worrying about side effects.

Part of the rationale for this is to make routing definitions look a little cleaner. Much like _form, which have represent something singular. Many instances of this however will be nullified by 📌 Use PHP attributes for route discovery Needs review as they will no longer need a pointer to _controller. This will still be still useful for projects extending/reusing controllers, or creating routing entries by other means.

Proposed resolution

Update controllers to have a single routable method, via __invoke.
Deprecate existing methods.

Paired with 📌 Use PHP attributes for route discovery Needs review , our controllers could only require this boilerplate:

#[Route('/test')]
final class TestController extends ControllerBase {
  public function __invoke(): Response|array {
    return ['#plain_text' => 'Hello world!'];
  }

Remaining tasks

Decide if we want to do this.

Many LOCs will be affected. Subsystem maintainer approval?

Depending on the size of task, it may make sense to break into subsystems.

Decide to do controllers with single methods AND controllers with multiple methods all in one go. Or, break into two: single first, then multiple. Perhaps dependent on quantity instances (TBD)

Most instances are for tests. Some of which have controllers with quite a lot of methods. Porting these will introduce a lot of extra files and boilerplate. Should we port these also?

In the case of controllers with multiple routeable methods, common functionality needs to be identified to be converted to traits, or other, on a case-by-case basis.

Implement.

User interface changes

Nil.

API changes

Many methods deprecated, immediately replaced with a consistently named method. Or moved to a different class entirly

Data model changes

Nil

Release notes snippet

TBD

Analysis

Analysis of controller/method, sorted by method count

Non test modules

Test modules

📌 Task
Status

Active

Version

11.0 🔥

Component

routing system

Created by

🇦🇺Australia dpi Perth, Australia

Live updates comments and jobs are added and updated live.
  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

  • Needs release manager review

    It is used to alert the release manager core committer(s) that an issue significantly affects the overall technical debt or release timeline of Drupal, and their signoff is needed. See the governance policy draft for more information.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024