- Drupal 8 Module Development
- Daniel Sipos
- 219字
- 2021-07-02 12:22:42
Redirecting from a Controller
In this chapter, we wrote a Controller that returns a render array. We know from the preceding chapter that this is picked up by the theme system and turned into a response. In Chapter 4, Theming, we will go into a bit more detail and see how this process is done. However, this render pipeline can also be bypassed if the Controller returns a response directly. Let's consider the following example:
return new \Symfony\Component\HttpFoundation\Response('my text');
This will bypass much of that processing and return a blank white page with only the "my text" string on it. The Response class we're using is from the Symfony HTTP Foundation component.
However, we also have a handy RedirectResponse class that we can use, and it will redirect the browser to another page:
return new \Symfony\Component\HttpFoundation\RedirectResponse('node/1')
The first parameter is the URL where we want to redirect. Typically, this should be an absolute URL; however, browsers nowadays are smart enough to handle a relative path as well. So, in this case, the Controller will redirect us to that path.
- jQuery Mobile Web Development Essentials(Third Edition)
- Intel Galileo Essentials
- Java程序設計實戰教程
- Beginning C++ Game Programming
- Python爬蟲開發:從入門到實戰(微課版)
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Java Web及其框架技術
- 程序員考試案例梳理、真題透解與強化訓練
- Getting Started with SQL Server 2012 Cube Development
- CKA/CKAD應試教程:從Docker到Kubernetes完全攻略
- RabbitMQ Cookbook
- Learning ArcGIS for Desktop
- Kotlin開發教程(全2冊)
- Web前端應用開發技術
- Spring+Spring MVC+MyBatis從零開始學