Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By smiletrl on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Description:
Motivation:
After we finished the change record of ControllerBase class available for routing controllers to minimize boilerplate code. We realized the remaining interface 'ControllerInterface' will be confusing the new class "ControllerBase":
- The two names could imply there're some connections between the two interface/class, e.g., ControllerBase implements ControllerInterface. But in fact, they dont' have such direct associations, although they could serve the "Routing Controller" in different scenarios.
- Code of interface ControllerInterface doesn't actually/literally do anything with "Controllers" or "Routing Controllers". It simply contains a factory method to inject the $container. See WSCCI Conversion Guide for more information about "Routing Controllers". So, we need to keep this interface more generic other than wordly coupled with 'Controllers'.
Solution
Rename interface "ControllerInterface" to "ContainerInjectionInterface" and move its namespace from 'Drupal\Core\Controller' to 'Drupal\Core\DependencyInjection'.
Code change
Before this change, it's
/**
* @file
* Contains \Drupal\book\Controller\BookController.
*/
namespace Drupal\book\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerInterface;
/**
* Controller routines for book routes.
*/
class BookController implements ControllerInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
Now, it becomes:
/**
* @file
* Contains \Drupal\book\Controller\BookController.
*/
namespace Drupal\book\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
/**
* Controller routines for book routes.
*/
class BookController implements ContainerInjectionInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
Impacts:
Module developers