官术网_书友最值得收藏!

The route

Inside our module, we will need to create our routing file that will hold all our statically defined routes. The name of this file will be hello_world.routing.yml. By now, I assume that you understand what the deal is with the file naming conventions in a Drupal 8 module. However, in any case, this is another YAML file in which we will need to put the YAML formatted data:

hello_world.hello:
path: '/hello'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
_title: 'Our first route'
requirements:
_permission: 'access content'

This is our first route definition. It starts with the route name (hello_world.hello) followed by all the necessary info about it, below, in a YAML formatted multidimensional array. The standard practice is to have the route name start with the module name it is in, followed by route qualifiers as needed.

So, what does the route definition contain? There can be many options here, but, for now, we will stick with the simple ones that serve our purpose. For more info about all that you can configure, visit the relevant documentation page on https://www.drupal.org/docs/8/api/routing-system/structure-of-routes. It is a good resource to keep on hand.

First, we have a path key, which indicates the path we want this route to work on. Then, we have a defaults section, which usually contains info relevant to the handlers responsible for delivering something when this route is accessed. In our case, we set the controller and method responsible for delivering the page and the title of the page. Finally, we have a requirements section, which usually has to do with conditions that need to be met for this route to be accessible (or be hit)--things such as permissions and format. In our case, we will require users to have the access content permission, which most visitors will have. Don't worry, we will cover more about access in a later chapter.

That is all we need for our first route definition. Also, note that the Drupal 8 routing system is essentially identical to that of Symfony, albeit with some modifications here and there, and although our route definition is done, we will now need to create the Controller that maps to it and can deliver something to the user.

Before we do that, let's look at an example of a very common routing requirement you will most likely have to use really soon. We don't need this for the functionality we're building in this chapter, so I won't include it in the final code. However, it's important that you know how this works.

主站蜘蛛池模板: 陇川县| 壤塘县| 攀枝花市| 望江县| 正宁县| 辽宁省| 和田市| 武义县| 斗六市| 长汀县| 个旧市| 图木舒克市| 平谷区| 辉县市| 通渭县| 星座| 平度市| 航空| 威海市| 崇左市| 丘北县| 琼结县| 乌鲁木齐市| 呼图壁县| 青冈县| 寿阳县| 丽江市| 九龙坡区| 丽江市| 惠州市| 南宁市| 剑川县| 桐乡市| 西贡区| 浦县| 松江区| 墨竹工卡县| 双柏县| 宝山区| 柞水县| 彭泽县|