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

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

  • 1.
    Exploring Drupal 8Routing System Surbhi Sriwal | Drupal Developer, Srijan Technologies #SrijanWW | @srijan
  • 2.
    #SrijanWW | @srijan SurbhiSriwal 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 Whatnext? ● 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
  • 5.
  • 6.
    #SrijanWW | @srijan Whatis 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 RoutingIn Symfony In Symfony we have #app/config/routing.yml blog_show: path: /blog defaults: {_controller: ControllerName:method}
  • 8.
    #SrijanWW | @srijan InDrupal 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’stoo 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:TheUnderlying 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 SymfonyHTTP 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 SymfonyHTTP 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 Drupal8 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 YMLfile 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 Howexactly hook_menu() was decoupled?
  • 18.
    #SrijanWW | @srijan Thiswas 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
  • 19.
  • 20.
    #SrijanWW | @srijan Wheneveryou 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 Whentwo 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 Ifyou 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 Insidethe 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 Youdon'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
  • 25.
  • 26.
  • 27.
  • 28.
    #SrijanWW | @srijan Whileworking 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
  • 29.
  • 30.
    #SrijanWW | @srijan Conclusion 1So 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 KnowMore Thank You @surbhisriwal www.srijan.net