- Drupal 8 Module Development
- Daniel Sipos
- 293字
- 2021-07-02 15:45:14
Route variables
A very common requirement is to have a variable route parameter (or more) that gets used by the code that maps to the route, for example, the ID or path alias of the page you want to show. These parameters can be added by wrapping a certain path element into curly braces, like so:
path: '/hello/{param}'
Here, {param} will map to a $param variable that gets passed as an argument to the controller or handler responsible for this route. So, if the user goes to the hello/jack path, the $param variable will have the jack value and the controller can use that.
Additionally, Drupal 8 comes with parameter converters that transform the parameter into something more meaningful. For example, an entity can be autoloaded and passed to the Controller directly instead of an ID. Also, if no entity is found, the route acts as a 404, saving us a few good lines of code. To achieve this, we will also need to describe the parameter so that Drupal knows how to autoload it. We can do so by adding a route option for that parameter:
options:
parameters:
param:
type: entity:node
So, we have now mapped the {param} parameter to the node entity type. Hence, if the user goes to hello/1, the Node with the ID of 1 will be loaded (if it exists).
We can do one better. If, instead of {param},we name the parameter {node} (the machine name of the entity type), we can avoid having to write the parameters option in the route completely. Drupal will figure out that it is an entity and will try to load that node by itself. Neat, no?
So keep these things in mind the next time you need to write dynamic routes.
- 微服務(wù)設(shè)計(jì)(第2版)
- Instant Testing with CasperJS
- SOA實(shí)踐
- SQL語言從入門到精通
- Internet of Things with the Arduino Yún
- Linux網(wǎng)絡(luò)程序設(shè)計(jì):基于龍芯平臺(tái)
- Python金融數(shù)據(jù)分析
- Java程序設(shè)計(jì)與實(shí)踐教程(第2版)
- Python數(shù)據(jù)分析(第2版)
- Getting Started with SQL Server 2012 Cube Development
- Oracle 18c 必須掌握的新特性:管理與實(shí)戰(zhàn)
- Mastering Linux Security and Hardening
- C指針原理揭秘:基于底層實(shí)現(xiàn)機(jī)制
- HTML5游戲開發(fā)實(shí)戰(zhàn)
- Spring Boot從入門到實(shí)戰(zhàn)