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

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.

主站蜘蛛池模板: 鹤岗市| 育儿| 牙克石市| 宝丰县| 金乡县| 临泉县| 龙井市| 佛坪县| 上林县| 文化| 齐河县| 吉木萨尔县| 璧山县| 新邵县| 同心县| 武乡县| 霍山县| 阜宁县| 丰城市| 偃师市| 莱州市| 湘乡市| 东明县| 彰武县| 宝清县| 东光县| 台中市| 色达县| 葫芦岛市| 永安市| 探索| 夹江县| 宜城市| 蓝田县| 阿坝县| 蓬莱市| 子长县| 禹城市| 崇礼县| 镇原县| 项城市|