Service approach

The service approach involves creating a tagged service and referencing that in the route definition as a requirement. There are a number of advantages to this method compared to the one we've just seen:

  • Allows you to encapsulate complex access logic in its own class
  • Allows you to inject dependencies and make use of them in calculating the access
  • Allows you to reuse the access checker on multiple routes

So, let's take a look at how we can implement this for our Hello World route. We will replace the preceding approach, but keep the goal of denying access to editors. However, to increase a bit, complexity, editors will be allowed if the Hello World salutation has not been overridden via the configuration form. If you recall, ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.