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

Which way to link?

As we saw, we have a number of ways to create links and URL representations, but when it comes to creating a link, which method should we use? There are advantages and disadvantages to each one.

When it comes to URL, as mentioned, it's a good idea to stick to hardcoding routes rather than URIs. However, if you are working with dynamic data, such as user input or stored strings, the other methods are perfectly valid. I recommend that you look at the Url class in detail because you will be using it quite a bit as you develop Drupal 8 modules.

Regarding the actual links, using the service to generate a link means that you are creating a string at that point in the code. This means that it cannot be altered later in the process. However, using the Link class falls nicely in line with the entire render array rationale of delaying the actual generation to the last possible moment. We will talk more about render arrays later on. So, the choice you make depends on the link you need to generate and your answer to the following question: is the link something that might have to be alterable by other modules/themes? If so, proceed with the render array. Otherwise, you might consider generating if you can inject the service properly.

When it comes to entities, it's always better to use the helper methods on the base entity classes to generate links and URLs to these entities. We will talk more about entities later in this book.

主站蜘蛛池模板: 鲁山县| 麟游县| 南靖县| 贵南县| 金平| 和平县| 北宁市| 离岛区| 叙永县| 龙岩市| 祥云县| 通城县| 托克托县| 汉阴县| 明溪县| 贵港市| 图木舒克市| 兖州市| 环江| 北海市| 汉中市| 鄂托克前旗| 新野县| 十堰市| 铜鼓县| 曲阜市| 南召县| 郯城县| 泗阳县| 桑植县| 三门峡市| 渑池县| 商都县| 阳谷县| 哈尔滨市| 盘山县| 静宁县| 凤冈县| 安岳县| 兴安盟| 毕节市|