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

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.

Typically, when returning redirect responses, you'll want to use a child class of RedirectResponse. For example, we have the LocalRedirectResponse and TrustedRedirectResponse classes which both extend from SecuredRedirectResponse. The purpose of these utilities is to ensure that redirects are safe.
主站蜘蛛池模板: 昌黎县| 芦山县| 衡阳县| 汾阳市| 新邵县| 尖扎县| 育儿| 庆阳市| 寿阳县| 商丘市| 安西县| 玉溪市| 淳化县| 兴隆县| 杨浦区| 乾安县| 翼城县| 淳安县| 武川县| 齐河县| 沁阳市| 广州市| 韶山市| 巴里| 东乡族自治县| 微山县| 永泰县| 安陆市| 昭苏县| 天门市| 云龙县| 体育| 旌德县| 湟源县| 大理市| 会理县| 建阳市| 石首市| 都江堰市| 寻乌县| 大宁县|