Routing is a crucial component in Drupal module development as it determines how URLs map to content or functionality. This lesson delves into defining routes within a .routing.yml file, providing a pathway for your module to direct user requests to the appropriate content or logic.
Understanding Drupal's Routing System
Drupal's routing system utilizes Symfony components to define URL paths and associate them with specific controllers that handle requests. It replaces the old hook_menu approach, preferring a more structured YAML-based configuration. Routes are defined in .routing.yml files, offering a clear, declarative way to manage site navigation and content delivery.
Creating a .routing.yml File
Each custom module requires a .routing.yml file to define its routing specifics. This file is placed within the module's directory, alongside other configuration files like .info.yml. For our example with a module named "Hello World," this will be hello_world.routing.yml.
Begin by creating this file within the modules/custom/hello_world directory.
Defining Basic Routes
A route in Drupal is defined with a unique name and several key attributes. Below is a sample structure for a route:
hello_world.content:
path: '/hello'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloWorldController::content'
_title: 'Hello World'
requirements:
_permission: 'access content'
- Route Name:
hello_world.contentuniquely identifies the route within your application. It's a good practice to prefuse the module name, ensuring global uniqueness. - Path:
/hellospecifies the URL path that users navigate to for accessing the route. This example would be accessible athttp://yourdrupalsite.com/hello. - Defaults: Within this key,
_controllerdefines the method in the controller that handles the request. The_titleprovides a title for the page when accessed. - Requirements: These conditions determine who can access this route using permissions like
access content.
Creating Controllers for Routing
The route's _controller is crucial as it dictates the logic executed when the route is accessed. Here's how you can set up a basic controller:
Step 1: Create the Controller
Create a file named HelloWorldController.php within a src/Controller directory in your module folder:
namespace Drupal\hello_world\Controller;
use Drupal\Core\Controller\ControllerBase;
class HelloWorldController extends ControllerBase {
public function content() {
return [
'#markup' => $this->t('Hello, World! Welcome to my custom Drupal module.'),
];
}
}
Here, the controller returns a simple 'Hello, World!' message when the route is accessed. This can be expanded into more complex operations as needed.
Testing Your Route
Once your route and controller setups are complete, it's essential to test them:
- Clear caches: Routes require cache clearance before changes are recognized. Run:
drush cr
This clears the cache, ensuring Drupal recognizes the new route configuration. - Access the route: Open your browser and navigate to
/helloon your site. Verify the 'Hello, World!' message appears, confirming successful routing.
Conclusion
Defining routes in a .routing.yml file is a foundational step in crafting dynamic and navigable content in your Drupal site. This lesson covered setting up basic routes and using controllers to handle requests. Successfully implementing this opens the way for more complex interactions and functionalities.
In the next lesson, we will explore Setting up URL paths for routes, where you'll learn about creating dynamic routes that handle variable data inputs from URLs. Let's continue our journey!