O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

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

73 visualizações

Publicada em

Speaker - Surbhi Sriwal

This session is a deep dive into understanding all the technical aspects of Drupal 8 - specifically the Routing System.

Key Takeaways:

-Understand how Drupal 8 makes use of Symfony’s routing system using yaml and controllers
-Know how is D8 routing is different from D7
-What you can do with the D8 routing system
-Understand the execution precedence of same routes
-Learn how to structure Drupal 8 routes
-Get a hands-on demo of how to define your own routes

Join the webinar to get a better understanding of the Drupal 8 routing system and what it is capable of.

View all webinars at: www.srijan.net/webinar/past-webinars

Publicada em: Educação
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

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

  1. 1. Exploring Drupal 8 Routing System Surbhi Sriwal | Drupal Developer, Srijan Technologies #SrijanWW | @srijan
  2. 2. #SrijanWW | @srijan Surbhi Sriwal Drupal Developer at Srijan Technologies https://www.drupal.org/u/surbz @surbhisriwal Whoami
  3. 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. 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
  5. 5. #SrijanWW | @srijan Introduction
  6. 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. 7. #SrijanWW | @srijan Routing In Symfony In Symfony we have #app/config/routing.yml blog_show: path: /blog defaults: {_controller: ControllerName:method}
  8. 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. 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. 10. #SrijanWW | @srijan D8: Goodbye hook_menu()
  11. 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. 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. 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. 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. 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. 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. 17. #SrijanWW | @srijan How exactly hook_menu() was decoupled?
  18. 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
  19. 19. #SrijanWW | @srijan Structure Of Routes
  20. 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. 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. 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. 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. 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
  25. 25. #SrijanWW | @srijan Enough Talk
  26. 26. #SrijanWW | @srijan Show Me The Code
  27. 27. #SrijanWW | @srijan Showcase
  28. 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
  29. 29. #SrijanWW | @srijan https://github.com/surbz/routing You can checkout this code from github
  30. 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. 31. #SrijanWW | @srijan Know More Thank You @surbhisriwal www.srijan.net