In Drupal's modular ecosystem, routes and controllers work hand-in-hand to guide user requests to the necessary functionality. This lesson focuses on linking routes to controller classes and methods. This connection allows us to serve specific responses based on user inputs via the URL, ensuring your web application operates smoothly and efficiently.
Understanding the Role of Controllers
Controllers in Drupal are PHP classes that define methods to handle HTTP requests for routes. They execute business logic and return responses, typically as render arrays, which Drupal processes to create HTML content. Each method within a controller is linked to a specific route, giving it the flexibility to handle different parts of your application seamlessly.
Controllers act as the "brain," interpreting requests from URLs defined in routes and delivering the appropriate output based on logic and data retrieval.
Defining Routes and Linking to Controllers
The .routing.yml
file is where you define routes and specify the controller methods they link to. Let's create a route for our "Hello World" module that renders the homepage with a welcome message:
hello_world.homepage: path: '/home' defaults: _controller: '\Drupal\hello_world\Controller\HelloWorldController::homepage' _title: 'Welcome to Our Site' requirements: _permission: 'access content'
- Route Name: The route is uniquely identified as
hello_world.homepage
. - Path: A simple static path
/home
navigates to the homepage functionality. - Controller: The
_controller
specifies the methodhomepage
in theHelloWorldController
, linking the route to its execution logic.
Creating a Controller Class
In order to connect routes to functionality, you'll need to implement the corresponding methods in a controller class. Here’s how to set up a basic controller class in your module:
Step-by-Step Implementation
- Create the Controller Directory: Within your module directory
modules/custom/hello_world
, create asrc/Controller
folder if it doesn't already exist. - Create the Controller File: In the
src/Controller
directory, create a file namedHelloWorldController.php
. - Define the Controller Class: Implement the controller class with the method that corresponds to your route:
namespace Drupal\hello_world\Controller; use Drupal\Core\Controller\ControllerBase; class HelloWorldController extends ControllerBase { public function homepage() { return [ '#markup' => $this->t('Welcome to our Drupal site! Enjoy exploring our content.'), ]; } }
This simple controller returns a static message when the /home
route is accessed, fulfilling the request configured by the route.
Linking Multiple Routes to Methods
One of the strengths of Drupal's routing system is its ability to link multiple routes to various methods within a single controller. Extend your controller with additional methods to handle distinct requests:
class HelloWorldController extends ControllerBase { public function homepage() { return [ '#markup' => $this->t('Welcome to our Drupal site! Enjoy exploring our content.'), ]; } public function about() { return [ '#markup' => $this->t('About Us: We strive to provide the best Drupal solutions.'), ]; } }
Add a new route in .routing.yml
for the about
method:
hello_world.about: path: '/about' defaults: _controller: '\Drupal\hello_world\Controller\HelloWorldController::about' _title: 'About Us' requirements: _permission: 'access content'
This layout demonstrates the flexibility offered by centrally managing application logic via controllers.
Testing Route Links to Controllers
Confirm that your route-controller connections work correctly:
- Clear Cache: Run
drush cr
to ensure new routes are recognized by Drupal. - Test Routes: Navigate to the URLs such as
/home
and/about
to verify that the respective controller methods render the intended content. - Check for Errors: Review the site's logging or watch for PHP errors, ensuring there are no issues with the controller logic.
Successful tests indicate that your routes effectively communicate with controller classes to deliver dynamic site content.
Conclusion
Successfully linking routes to controller classes or methods is a fundamental step in custom Drupal module development. This connection allows you to create structured, dynamic web applications that respond appropriately to user requests. Controllers enable centralized management of business logic, enhancing your site's scalability and maintainability.
Next, we will advance to Building a controller class extending ControllerBase, where we'll further explore building robust controllers by using advanced techniques and extending existing functionality. Stay tuned as we continue developing your Drupal skills!