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

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 Response('my text');

This will bypass much of Drupal, in fact, 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 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 also, as in the preceding example. So, in this case, the Controller will redirect us to that path.

主站蜘蛛池模板: 宝应县| 霸州市| 嵊泗县| 阿巴嘎旗| 天等县| 子洲县| 海安县| 东港市| 余江县| 姜堰市| 广德县| 方城县| 台中县| 广元市| 斗六市| 江西省| 通州区| 石台县| 安乡县| 沙坪坝区| 新乐市| 德州市| 平乐县| 靖江市| 洪泽县| 南充市| 松溪县| 潞西市| 磴口县| 玉门市| 绵竹市| 清新县| 三亚市| 肥城市| 葫芦岛市| 鄂托克旗| 吉首市| 皮山县| 浮梁县| 富平县| 曲周县|