 Good afternoon everyone, I will be talking about let us take the best route exploring to the next session. So during my talk I will be using some of the screenshots and demonstrating things with the help of some code. So while the talk if you feel like there is some readability problem you can stop me for a while and we can connect on it in a better way. My name is Surbhi Srival and I work with Srijan Technologies. So Srijan has its headquarters back in India New Delhi and we have colleagues working all over the world. So at Srijan I am working primarily with Drupal and I am currently working as a backend developer for Drupal 7 and Drupal 8. On Twitter I go by the handle Surbhi Srival and if you want to locate my profile on drupal.org you can either hit that link over there or you can search me in my full name. So before we begin I would like to know how many of you have got a chance to work with MBC frameworks, anybody from the crowd and specifically with symphony, great. So the agenda I would be covering the overview of this talk with the help of an introductory example. We will be talking about how Drupal 8 has taken its routing mechanism from symphony and I will be talking about HTTP kernel then we will see that there was a lot of tight coupling in Drupal 7 and we will see how Drupal 8 is different from Drupal 7 when it comes to routing, how Drupal 8 makes user YML and controllers, we will be studying some of the structure of routes and what would happen, like how Drupal treats a request which is coming in and there are multiple routes for a single request which precedents would be taken. I will be talking about that then we will check some using some like symphony provides us some basic things but Drupal is capable of doing a lot more than that. So we will check about it and I will show you some code in the end. If I talk in generic terms what a route is, like everybody has travelled to this place to attend this session, we have started from some source point to reach some destination. So each one of us has taken a route, we might have taken the same path or we might have taken different paths, in Drupal, route is defined as a way that would return some sort of content. So this content could be a JSON or it could be an HTML, for example slash node which is the default front page of a fresh Drupal installation is a route, node add which opens up a node add form is a route, node nid edit, all these are routes. Just a very basic overview of how things are with symphony. So there is app folder and inside the config directory we have a routing via mill file and if you can see block show that is the name of a route which is mapped to the path slash block and the corresponding controller is controller name and method. This is a very basic how symphony uses a routing. So when we were with Drupal 7, like before we started working with Drupal 8, there was a function called hook underscore menu and in this function routing was handled. Not just routing, this function has other responsibilities to handle too that is most of the visible navigation on the site, access control and all these things. So what happens? There is a table called menu router table which stores all the paths and as soon as the request comes in, the corresponding request is fetched from this table. So if anyone of you have not got a chance to look inside what this table contains, we can do this right now. Let me log into my database. Yeah. So a D7 database from here. So if you see these are some of the fields which the menu router table has got and you can see the column names, access callbacks, access arguments, path callbacks, page callback, page arguments. And let's see what this table has in store for us. So if you see these are the paths, admin slash appearance, admin slash appearance defaults. So whenever a request is coming in, the corresponding path is fetched. And the route is returned. So all these things like access callbacks, then the visible navigation, along with routing, there was a lot of a tight coupling when we were working with Drupal 7. Yeah, so anybody who is coming from an MVC background, it was really difficult for him to understand what is happening behind the scenes. So Drupal 7 has reduced this tight coupling. And it has just said goodbye to hook menu. So what Drupal 7 uses then? The routing mechanism in Drupal 7 has been borrowed from symphony. And it makes use of HTTP kernel, where this is located. If you go to the Drupal folder inside the vendor directory, there is a symphony directory and there you can find the HTTP kernel in case you want to dig deeper into it. And how it works, I'm going to explain you here. So in the diagram, you can see there are four blocks, a request block, which is generating a response in the form of HTML or JSON. And how request is generating a response, it's taking use of the HTTP kernel and routing. So let's focus on the request block first. A request is coming in. This request could be a post request or a get request. It could come in from, let's say, a mobile device or a laptop. And this request is passed to the HTTP kernel. So HTTP kernel would take this request and ask other systems to produce some sort of output, yeah? So if there is some access check defined, Drupal would check that the accessing user has access to the page which he has requested. If yes, a 200 status code is returned and the user would be presented with HTML or JSON. If not, a 404 status code would be returned and access denied would be presented or page not found. All these things, how is Drupal 8 actually doing this? So Drupal 8 takes the help of routing.yml file and controller. So what is ayml file? So ayml file, we can say that it tells Drupal how to behave when a particular path is encountered. As you can see in the screenshot, drupalcam.routing.yml. So if we go by the convention, we follow module name, then routing.yml. So if you see there, drupalcam.content, this is basically a path name which is defined with the combination of module name and path name. And it is mapped to the path slash example. So whenever a user is trying to access slash example, the permission access content would be checked on this accessing user. And if the access is granted, then example class controller would be called and the content method would be in boot. So here the PSR4 mechanism is used where slash drupal module name controller, example controller class name, and method is mapped to slash drupal src folder. So what is a controller? A controller is a piece of code which helps to return the output. So we have seen how the tight coupling of drupal 7 has been reduced in drupal 8 because drupal 8 makes use of different files. So if you see, this is the structure of a route, what all things a route can specifically have. So requirements, requirement is something which tells that these are the conditions which should be met before a route is generated. And if you see options, so these are all optional parameters. We can define them or we can just escape them. Not just the static, but we do have dynamic routes. For example, slash blog slug, this is a dynamic route. It is mainly a sort of a wildcard character that can be passed. And the first argument, the first parameter of this route cannot be dynamic. So for example, if you say slash slug slash blog that is not allowed, then we can also limit the type of the slug that can be passed. For example, if here you can see I have limited the slug entry, en or fr. So if a user hits en or fr, then only he would be shown with the requested page. So what would happen if drupal has a request coming in and it has got multiple paths for the same request? So the first path which loads that wins. For example, you can see here there is a path defined drupalcam.blogshow. And it has the rule slash blog slash slug. So if I define another path with, let's say, a name event show and the same path slash blog slash event. So if the request is coming in, it blog show would be loaded first. To limit this, we can set permission check on slug. We can define the values of slug. Like here I have defined en or fr. We can also define if the slug can be an integer value or it can be anything n. We can use regerits expressions over here. Not just this, drupal also provides the ability to load the complete entity type object. So for example, user, this is the name of an entity type. So if we are using, we want to use user object. We want to make use of complete user object inside our controller. We can do that. And we just have to type in the user ID over there. And drupal would handle the loading of this object automatically by using the help of entity load function. So if the user ID does not exist, drupal would give a 4.4. As you can see, this is a function show user in controller. And this dollar user, here the complete user object would be loaded. So about all this of talk, I would like to show some code. So this is a very fresh, clean, drupalate installation. And I'm logged in as an administrator on this site. So if I hit the path slash example, this page is being displayed. This is a simple title. And this is a markup which is returned from the controller. So if you see, I have defined a role administrator with this path. So if I'm an administrator, this path would be presented. Now let's see what happens if a non-admin user tries to access. So as you can see, there is access denied because of this thing coming in. And if I hit, let's say, a non-existent path, then it would be a page not found. So you see, there is a difference between this and this. So talking about dynamic routes. So this is a path slash block. And I have passed in a value over here. So I have defined it to be dynamic. It could be an integer, a string, or a combination of both. This is because I have not defined the value of slug over here right now. So if I hit a string or an integer, it would load this page. Now let's see what happens when we try limiting this value. So this is basically a useful command. I'll be talking about it in a bit later. So now I have limited the value. The integers would be allowed. So if I say, let's say, pass a string, it would give a page not found. So similarly, we can define different type of routes over like this. And dynamic routes. Let's have a quick look at how to use entity type objects. So this is a path and show user slash one. This one is the user ID of admin. And in the controller, you would have access to all these, the complete user object. You can avail the benefits of it. And about, so whenever we are working with routing, at times you would find that the changes you are making to the code are not just simply reflecting. So for that, I would just to mention that Drupal stores all the things in the router table. And every time a path is being generated, it rebuilds the router table. So instead of clearing the complete caches, you can use this helpful command. And it would rebuild the router table and reflect your changes. If you're using Drush, then you would be using Drush environment Drupal service router builder rebuild. And in case you're using Drupal console, then you can use router rebuild command. So I have pushed this code to this URL. You can check this out from this URL. So we have seen that how Drupal 8 has removed the tight coupling which we had in Drupal 7. We have seen how HTTP kernel has came in as an advantage and how it functions. And this is just a very brief step. You have to define a route name. Then you have to attach a path to it. You can decide whether the URL could be static or dynamic. And you can use regex expressions to limit the type of entries. So any questions? Yeah. Basically, when we are in Drupal 8, all the paths would be stored in the router table. So coming specifically to the speed part, I have not specifically tested it. I would have to look into it and maybe we can connect. Yeah, it definitely creates an entry. So when I was working with this, the changes were not reflecting immediately. So I got a chance to look inside. And I found that, yeah, it's creating some entries in the router table. And it's rebuilding the router table every time. Yeah, I got your question. So defaults is basically an array. It's the name of an array. And as the name suggests, defaults, anything which has to be passed as a default value, that should go inside that array. So I hope that's clear. That's a valid point. Yeah, take that. Thank you. Any more questions here? OK, localization. So I have not worked with multilinguals. Maybe I can check it. And so you can leave your question on my Twitter handle. And I can connect with you on that. So yeah, so the delivery quantum part is handled by the controller section. So in the controller, you would have to define the type of markup or the type of output that has to be returned. If you want to return JSON, so a duplicate has got JSON classes for that. You can make use of them and return the JSON objects. And if you want to return markups, then you can return markups as well. So for that, firstly, you would have to make the use of the use statement to use the JSON and HTML classes. And then in the controller, you can use anything, like if a statement or switch case is anything, sort of that. Yeah, so we do have a disable cache option. In the optional parameters, we do have it. Yeah, we can disable that. But of course, as the gentleman suggested, there is a performance issue of completely disabling the cache. So we might have to reconsider that also. So you can look at my session at the CURL. And in case you have any more questions, you can connect with me on my Twitter handle, so v3.gov. And thank you for being a great audience. I do have another session in room number ELG05 at 1615. I'll be talking about amping up a Drupal 8 site if you're interested in amping up a Drupal 8 site, accelerating mobile pages.