Understanding Drupal 8 Routes and Controllers
Two weeks ago I wrote about routes and controllers in the introduction to namespaces. This week we are going to take a much closer look at routes and controllers.
So, what exactly is a route and a controller?
When you create a custom page in Drupal with code, you need both a route and a controller. You define the URL for the page with the route. And then you create a controller for that page. This will be responsible for building and returning the content for the page.
Routes
A route determines which code should be run to generate the response when a URI is requested. It does this by mapping a URI to a controller class and method. This defines how Drupal deals with a specific URI.
Controllers
Controllers take requests or information from the user and decide how to handle the request. For the example module in this tutorial, the controller is responsible for generating the content and returning it for the page.
Read on to learn more about routes and controllers in Drupal 8 modules...