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

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.

主站蜘蛛池模板: 凤阳县| 青铜峡市| 祥云县| 如东县| 西乌| 攀枝花市| 清丰县| 晋中市| 泸水县| 阿拉善左旗| 巴里| 万山特区| 惠州市| 靖安县| 乳源| 桂东县| 江阴市| 都匀市| 榆社县| 莆田市| 吴忠市| 万安县| 略阳县| 滦南县| 横峰县| 平乐县| 上虞市| 南部县| 平顺县| 含山县| 周宁县| 桂东县| 当雄县| 鸡泽县| 辽宁省| 宿州市| 大连市| 吴江市| 泰来县| 东辽县| 林甸县|