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

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 for 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 your point in the code. This means that it cannot be altered later in the process. On the other hand, 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.

主站蜘蛛池模板: 柳州市| 绍兴市| 赫章县| 渑池县| 武穴市| 康乐县| 敖汉旗| 黄骅市| 阿图什市| 吉安县| 永顺县| 绥棱县| 久治县| 博野县| 抚宁县| 崇州市| 娄底市| 西盟| 都江堰市| 万州区| 海林市| 甘孜| 遂平县| 宁蒗| 南郑县| 霍邱县| 应城市| 海淀区| 海林市| 广安市| 余干县| 铜川市| 江孜县| 西乌珠穆沁旗| 门源| 桦甸市| 余姚市| 诏安县| 荆州市| 瓦房店市| 隆子县|