- Drupal 8 Module Development
- Daniel Sipos
- 263字
- 2021-07-02 12:22:41
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.
- LabVIEW程序設(shè)計(jì)基礎(chǔ)與應(yīng)用
- arc42 by Example
- HTML5 移動(dòng)Web開(kāi)發(fā)從入門(mén)到精通(微課精編版)
- AngularJS Web Application Development Blueprints
- 我的第一本算法書(shū)
- Visual Basic程序設(shè)計(jì)教程
- Learning Python Design Patterns(Second Edition)
- Backbone.js Blueprints
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- 組態(tài)軟件技術(shù)與應(yīng)用
- Learning Continuous Integration with TeamCity
- 并行編程方法與優(yōu)化實(shí)踐
- Developing Java Applications with Spring and Spring Boot
- Apache Solr for Indexing Data
- Python面向?qū)ο缶幊蹋ǖ?版)