2

I use Event Listener from this SO answer to redirect users with specific roles. But now, for one role I have to redirect user to external link, with some POST data.

Since in onKernelResponse() I have to set $event response and RedirectResponse can't do forward with POST (at least that's what I've been told), I'm a bit lost here. How should I do it?

Community
  • 1
  • 1
b174008
  • 285
  • 3
  • 13

1 Answers1

3

In HTTP 1.1 you have 307 status code which says request should be repeated. So you can create Http Requestwith this code.

301/302 won't work for POST. The other way is to use CURL but it's different because you use other client then.

10.3.8 307 Temporary Redirect

The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.

If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

The code:

$response = new RedirectResponse($this->router->generate('your route'), Symfony\Component\HttpFoundation\Response::HTTP_TEMPORARY_REDIRECT);
Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • Thanks. Two questions: 1. So I have to create a route for external address? 2. The whole request must be repeated? Because I must set POST data before... – WRonX Jun 20 '16 at 07:40
  • 307 says that request should be repeated by browser you don't do it on your own. 2. You don't need create route you can pass string with url to first param of RedirectResponse – Robert Jun 20 '16 at 08:15
  • OK, I get it. So that's unfortunately not what I wanted, I must change data in the request... Anyway, thanks for your help! – WRonX Jun 21 '16 at 09:04
  • 1
    This saved me today. Thank you @Robert I was looking for something similar from a few hours. – Roydon D' Souza Dec 18 '19 at 21:10