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

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.

主站蜘蛛池模板: 定州市| 革吉县| 雷山县| 东辽县| 陆川县| 黑山县| 牙克石市| 当雄县| 海林市| 噶尔县| 桓台县| 平塘县| 苏尼特右旗| 古交市| 枝江市| 曲靖市| 正宁县| 绍兴县| 峨眉山市| 辽阳县| 利川市| 斗六市| 克东县| 邵东县| 南部县| 延庆县| 绥中县| 土默特右旗| 古田县| 崇州市| 金堂县| 富蕴县| 大渡口区| 汝南县| 且末县| 师宗县| 沈丘县| 新干县| 洛阳市| 江都市| 山东|