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

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.
主站蜘蛛池模板: 龙门县| 南阳市| 六安市| 四子王旗| 新泰市| 阿荣旗| 昌平区| 伊宁县| 灵寿县| 冷水江市| 丹东市| 赤城县| 瑞金市| 米脂县| 尉氏县| 巫溪县| 巴林右旗| 白玉县| 乌拉特后旗| 扶风县| 昌图县| 茂名市| 泾川县| 三门峡市| 永州市| 梅州市| 新建县| 汕尾市| 建湖县| 无棣县| 永康市| 宁海县| 黔东| 丽水市| 广平县| 大荔县| 昌都县| 来凤县| 山阳县| 登封市| 阳泉市|