SlideShare a Scribd company logo
Exploring Drupal 8 Routing
System
Surbhi Sriwal | Drupal Developer, Srijan Technologies
#SrijanWW | @srijan
#SrijanWW | @srijan
Surbhi Sriwal
Drupal Developer at Srijan Technologies
https://www.drupal.org/u/surbz
@surbhisriwal
Whoami
#SrijanWW | @srijan
Srijan | Global Open Source leaders
15
Years of
profitable Growth
200+
Largest Drupal
Company in Asia
Pacific Region
45+
Certified
Drupal Engineers
12
Multi-year Global
Customers
Asia’s Largest & Top 10 Worldwide
#SrijanWW | @srijan
Agenda
What next?
● Overview - with an introductory example
● Underlying mechanism of routing - HTTP Kernel
● How is Drupal 8 routing different from tightly coupled Drupal 7 hook_menu()
● How Drupal 8 makes use of yml and controller
● Structure of Routes
● Precedence of same routes
● Simple access checks using permissions and roles
● Dynamic routes and using params in routes
● Some Code
#SrijanWW | @srijan
Introduction
#SrijanWW | @srijan
What is a Route in Drupal?
In Drupal route is a way defined that would return some sort of content.
Example: /node the default front page is a route.
/node/add
/node/{nid}/edit
#SrijanWW | @srijan
Routing In Symfony
In Symfony we have
#app/config/routing.yml
blog_show:
path: /blog
defaults: {_controller: ControllerName:method}
#SrijanWW | @srijan
In Drupal 7 routing is handled by hook_menu().
Apart from just routing it had many other responsibilities to do
● Routing
● Access Control
● Visible Navigation
In Drupal 7 we have menu_router table
This table stores all the paths.
As soon as the request comes - the path is fetched from this table and corresponding
request is fetched.
How is it different from Drupal 7?
#SrijanWW | @srijan
It’s too much for a single function to handle and made some sort of a tight coupling
It looks a much of alien concept to developers coming from other MVC frameworks
How is it different from Drupal 7?
#SrijanWW | @srijan
D8: Goodbye hook_menu()
#SrijanWW | @srijan
D8:The Underlying Mechanism Of Routing
Drupal 8 has adopted its routing from Symfony.
What happens when drupal receives a request - HTTP Kernel
How Drupal does all this ?
#SrijanWW | @srijan
Symfony HTTP Kernel
You can have a look in the Drupal code where this Symfony Kernel lies by going to
<drupal folder>/vendor/symfony/http-kernel/
#SrijanWW | @srijan
Symfony HTTP Kernel
Drupal System uses this HTTP Kernel which gets the request and asks other systems to
produce o/p what are these other systems lets see
#SrijanWW | @srijan
Drupal 8 makes use of yml and controllers
Drupal System uses this HTTP Kernel which gets the request and asks other systems to
produce o/p
These other systems are
Routing File
.routing.yml
Controller
#SrijanWW | @srijan
<modulename>.routing.yml
YML file tells Drupal how to behave when a particular path is encountered.
If you want to specify a default value, you put it into the defaults array. In the same yml array
you specify the class and method connected with '::' to tell the system where to look up stuff.
YML Files
#SrijanWW | @srijan
ExampleController.php - Inside src/Controller/
Controller is a piece of code that is responsible
to generate the response object.
The key concept is the controller which gets
some parameters from the system and converts
them to the response.
So everything without an underscore is
considered to be a parameter to the controller.
Every other property has nothing to do with the
controller parameters and so are considered to
be internal and so have an underscore as prefix.
Controllers
#SrijanWW | @srijan
How exactly hook_menu() was decoupled?
#SrijanWW | @srijan
This was an introductory example now let's explore some more options along with routes so that
we can play around a bit later.
I have tried to cover the examples by referring Symfony docs along with Drupal docs and have tried
to bring a combination altogether.
Drupal can do a lot more than Symfony like access checks based on permissions and roles I have
tried to cover that too, let’s see them one by one ...
Structure Of Routes
#SrijanWW | @srijan
Structure Of Routes
#SrijanWW | @srijan
Whenever you have a {placeholder} in your route path, that portion becomes a wildcard: it
matches any value. Your controller can now also have an argument called $placeholder (the
wildcard and argument names must match).
Dynamic routes - path is a required param it can have static or dynamic arguments
Note that the first item of the path must not be dynamic
To limit - {slug} can be restricted by defining requirements
Dynamic Routes
#SrijanWW | @srijan
When two routes match the same URL, the first route that's loaded wins.
Unfortunately, that means that /blog/test will match the blogshow and not eventshow.
No good!
To fix this, add a requirement was added.
Precedence of same routes
#SrijanWW | @srijan
If you want to get access to an entity type you can use named params in the url.$user is the
name of an entity type.
If the userid does not exist drupal would give a 404.
Named parameters are not sanitized by default.This means that you really should sanitize all
values prior to use.
You can somewhat decrease a code injection security concern by specifying regex
requirements.
Inside the controller you will have access to the the complete user object
Named Params In Route
#SrijanWW | @srijan
Inside the controller you will have access to the the complete user object which can be used
as per the logic.
Named Params In Route
#SrijanWW | @srijan
You don't want to take care about loading an entity, see /user/{user} as example.
For entities you basically just use the name of the entity type and it will execute a entity_load
with the ID passed in the URL. Param converter manager
Upcasting
#SrijanWW | @srijan
Enough Talk
#SrijanWW | @srijan
Show Me The Code
#SrijanWW | @srijan
Showcase
#SrijanWW | @srijan
While working with routing If you update a routes file in Drupal 8 how do you clear the cache?
So instead of clearing caches you can rebuild the router table
If you use drush
drush ev 'Drupal::service("router.builder")->rebuild();'
to rebuild the routing information without clearing all the caches.
If you use drupal console
drupal router:rebuild
Some Useful Commands
#SrijanWW | @srijan
https://github.com/surbz/routing
You can checkout this code from github
#SrijanWW | @srijan
Conclusion
1 So the steps should be define a route name
Attach a path / url to it
Decide this url has to be static or dynamic
If dynamic we might want to sanitize this value before it passes to controller this can
be achieved by restricting the type of input like a regex
At times we may not want all the users to access routes and may want to limit access
We have seen how Symfony routing works using HTTP Kernel
and how Drupal 8 makes use of it.
We have seen Drupal can do more that what was available in Symfony.It can put
access checks based on roles and permissions which is something very specific to
Drupal itself.
3
2
Drupal 8 has decoupled the tight coupling we had with Drupal 74
#SrijanWW | @srijan
Know More
Thank You
@surbhisriwal
www.srijan.net

More Related Content

PPTX
PROGRAMMING IN JAVA-unit 3-part II
PPTX
PROGRAMMING IN JAVA -unit 5 -part I
PDF
Lecture1
PPTX
PROGRAMMING IN JAVA
PPTX
PROGRAMMING IN JAVA- unit 4-part I
PDF
Your First Scala Web Application using Play 2.1
PPTX
Building Apis in Scala with Playframework2
PDF
What's New In Laravel 5
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA -unit 5 -part I
Lecture1
PROGRAMMING IN JAVA
PROGRAMMING IN JAVA- unit 4-part I
Your First Scala Web Application using Play 2.1
Building Apis in Scala with Playframework2
What's New In Laravel 5

Similar to [Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Routing System (20)

PPTX
introduction to Laravel and its Basic and origin
PDF
Memphis php 01 22-13 - laravel basics
PDF
Final dependency presentation.odp
ODP
Drupal Best Practices
PDF
[Srijan Wednesday Webinars] Routing in Drupal 8: Decoupling hook_menu
PDF
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
PDF
13th Sep, Drupal 7 advanced training by TCS
PPTX
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
DOCX
Laravel
ODP
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
ODP
Coder Presentation Szeged
PPTX
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
PDF
[Srijan Wednesday Webinars] Simplifying Migration to Drupal 8
PDF
Ruby on rails RAD
PPTX
EdTechJoker Spring 2020 - Lecture 7 Drupal intro
PDF
Semalt: 3 Steps To PHP Web Page Scraping Web
PDF
Spring MVC
ODP
Pyramid tutorial
PDF
A deep dive into Drupal 8 routing
PPTX
Introduction to GraphQL
introduction to Laravel and its Basic and origin
Memphis php 01 22-13 - laravel basics
Final dependency presentation.odp
Drupal Best Practices
[Srijan Wednesday Webinars] Routing in Drupal 8: Decoupling hook_menu
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
13th Sep, Drupal 7 advanced training by TCS
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Laravel
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Coder Presentation Szeged
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
[Srijan Wednesday Webinars] Simplifying Migration to Drupal 8
Ruby on rails RAD
EdTechJoker Spring 2020 - Lecture 7 Drupal intro
Semalt: 3 Steps To PHP Web Page Scraping Web
Spring MVC
Pyramid tutorial
A deep dive into Drupal 8 routing
Introduction to GraphQL
Ad

More from Srijan Technologies (20)

PDF
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
PDF
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
PDF
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
PDF
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
PDF
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
PDF
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
PDF
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
PDF
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
PPTX
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
PPTX
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
PDF
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
PDF
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
PDF
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
PDF
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
PDF
[Srijan Wednesday Webinars] Building a High Performance QA Team
PDF
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
PDF
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
PDF
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
PDF
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
PDF
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
Ad

Recently uploaded (20)

PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
01-Introduction-to-Information-Management.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Trump Administration's workforce development strategy
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
Orientation - ARALprogram of Deped to the Parents.pptx
A systematic review of self-coping strategies used by university students to ...
Microbial disease of the cardiovascular and lymphatic systems
Yogi Goddess Pres Conference Studio Updates
Anesthesia in Laparoscopic Surgery in India
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
01-Introduction-to-Information-Management.pdf
Weekly quiz Compilation Jan -July 25.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Trump Administration's workforce development strategy
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program

[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Routing System

  • 1. Exploring Drupal 8 Routing System Surbhi Sriwal | Drupal Developer, Srijan Technologies #SrijanWW | @srijan
  • 2. #SrijanWW | @srijan Surbhi Sriwal Drupal Developer at Srijan Technologies https://www.drupal.org/u/surbz @surbhisriwal Whoami
  • 3. #SrijanWW | @srijan Srijan | Global Open Source leaders 15 Years of profitable Growth 200+ Largest Drupal Company in Asia Pacific Region 45+ Certified Drupal Engineers 12 Multi-year Global Customers Asia’s Largest & Top 10 Worldwide
  • 4. #SrijanWW | @srijan Agenda What next? ● Overview - with an introductory example ● Underlying mechanism of routing - HTTP Kernel ● How is Drupal 8 routing different from tightly coupled Drupal 7 hook_menu() ● How Drupal 8 makes use of yml and controller ● Structure of Routes ● Precedence of same routes ● Simple access checks using permissions and roles ● Dynamic routes and using params in routes ● Some Code
  • 6. #SrijanWW | @srijan What is a Route in Drupal? In Drupal route is a way defined that would return some sort of content. Example: /node the default front page is a route. /node/add /node/{nid}/edit
  • 7. #SrijanWW | @srijan Routing In Symfony In Symfony we have #app/config/routing.yml blog_show: path: /blog defaults: {_controller: ControllerName:method}
  • 8. #SrijanWW | @srijan In Drupal 7 routing is handled by hook_menu(). Apart from just routing it had many other responsibilities to do ● Routing ● Access Control ● Visible Navigation In Drupal 7 we have menu_router table This table stores all the paths. As soon as the request comes - the path is fetched from this table and corresponding request is fetched. How is it different from Drupal 7?
  • 9. #SrijanWW | @srijan It’s too much for a single function to handle and made some sort of a tight coupling It looks a much of alien concept to developers coming from other MVC frameworks How is it different from Drupal 7?
  • 10. #SrijanWW | @srijan D8: Goodbye hook_menu()
  • 11. #SrijanWW | @srijan D8:The Underlying Mechanism Of Routing Drupal 8 has adopted its routing from Symfony. What happens when drupal receives a request - HTTP Kernel How Drupal does all this ?
  • 12. #SrijanWW | @srijan Symfony HTTP Kernel You can have a look in the Drupal code where this Symfony Kernel lies by going to <drupal folder>/vendor/symfony/http-kernel/
  • 13. #SrijanWW | @srijan Symfony HTTP Kernel Drupal System uses this HTTP Kernel which gets the request and asks other systems to produce o/p what are these other systems lets see
  • 14. #SrijanWW | @srijan Drupal 8 makes use of yml and controllers Drupal System uses this HTTP Kernel which gets the request and asks other systems to produce o/p These other systems are Routing File .routing.yml Controller
  • 15. #SrijanWW | @srijan <modulename>.routing.yml YML file tells Drupal how to behave when a particular path is encountered. If you want to specify a default value, you put it into the defaults array. In the same yml array you specify the class and method connected with '::' to tell the system where to look up stuff. YML Files
  • 16. #SrijanWW | @srijan ExampleController.php - Inside src/Controller/ Controller is a piece of code that is responsible to generate the response object. The key concept is the controller which gets some parameters from the system and converts them to the response. So everything without an underscore is considered to be a parameter to the controller. Every other property has nothing to do with the controller parameters and so are considered to be internal and so have an underscore as prefix. Controllers
  • 17. #SrijanWW | @srijan How exactly hook_menu() was decoupled?
  • 18. #SrijanWW | @srijan This was an introductory example now let's explore some more options along with routes so that we can play around a bit later. I have tried to cover the examples by referring Symfony docs along with Drupal docs and have tried to bring a combination altogether. Drupal can do a lot more than Symfony like access checks based on permissions and roles I have tried to cover that too, let’s see them one by one ... Structure Of Routes
  • 20. #SrijanWW | @srijan Whenever you have a {placeholder} in your route path, that portion becomes a wildcard: it matches any value. Your controller can now also have an argument called $placeholder (the wildcard and argument names must match). Dynamic routes - path is a required param it can have static or dynamic arguments Note that the first item of the path must not be dynamic To limit - {slug} can be restricted by defining requirements Dynamic Routes
  • 21. #SrijanWW | @srijan When two routes match the same URL, the first route that's loaded wins. Unfortunately, that means that /blog/test will match the blogshow and not eventshow. No good! To fix this, add a requirement was added. Precedence of same routes
  • 22. #SrijanWW | @srijan If you want to get access to an entity type you can use named params in the url.$user is the name of an entity type. If the userid does not exist drupal would give a 404. Named parameters are not sanitized by default.This means that you really should sanitize all values prior to use. You can somewhat decrease a code injection security concern by specifying regex requirements. Inside the controller you will have access to the the complete user object Named Params In Route
  • 23. #SrijanWW | @srijan Inside the controller you will have access to the the complete user object which can be used as per the logic. Named Params In Route
  • 24. #SrijanWW | @srijan You don't want to take care about loading an entity, see /user/{user} as example. For entities you basically just use the name of the entity type and it will execute a entity_load with the ID passed in the URL. Param converter manager Upcasting
  • 28. #SrijanWW | @srijan While working with routing If you update a routes file in Drupal 8 how do you clear the cache? So instead of clearing caches you can rebuild the router table If you use drush drush ev 'Drupal::service("router.builder")->rebuild();' to rebuild the routing information without clearing all the caches. If you use drupal console drupal router:rebuild Some Useful Commands
  • 30. #SrijanWW | @srijan Conclusion 1 So the steps should be define a route name Attach a path / url to it Decide this url has to be static or dynamic If dynamic we might want to sanitize this value before it passes to controller this can be achieved by restricting the type of input like a regex At times we may not want all the users to access routes and may want to limit access We have seen how Symfony routing works using HTTP Kernel and how Drupal 8 makes use of it. We have seen Drupal can do more that what was available in Symfony.It can put access checks based on roles and permissions which is something very specific to Drupal itself. 3 2 Drupal 8 has decoupled the tight coupling we had with Drupal 74
  • 31. #SrijanWW | @srijan Know More Thank You @surbhisriwal www.srijan.net