1

Trying to build a controller method that can redirect and send the request data along with it. Controller/route redirect doesn't seem to have anything that can achieve this.

This question gave a new approach but I'm unable to figure out how to redirect to the specified page instead of rendering on the current.

$forward = Libraries::instance("controllers", "Main", [
     'request' => $this->request
]);
return $forward($this->request, ['action' => 'view']);

The route tests don't seem to provide any insight either, how can I go about achieving url redirect while still retaining the request data?

Community
  • 1
  • 1
Bankzilla
  • 2,086
  • 3
  • 25
  • 52
  • When you say redirect... do you mean an actual browser redirect and you want to preserve the data on the query string? For example, request comes into http://example.com/some/url?a=1&b=2 and you want to redirect it to http://example.com/another/url?a=1&b=2 ? – rmarscher Feb 04 '15 at 23:38
  • Browser redirect but I'm wanting to preserve both `$this->request->data` and `request->query`. So forwarding the current request data to another page – Bankzilla Feb 04 '15 at 23:44
  • 1
    Yeah, that's for doing an internal redirect. The HTTP specification provides no mechanism for redirecting with POST data (hence the POST-Redirect-GET pattern). However, you could still do the `$forward()` call and _then_ `return $this->redirect(...)` if the destination page doesn't require the same request context in order to render correctly. – Nate Abele Feb 05 '15 at 04:11
  • Can you explain the reason for the redirect a little more? Is this a redirect on the same domain? If so, you could probably use `history.replaceState()` in javascript on page load to change the url. You would probably want to check the user agent in php and perform 301 redirects for search engines such as google since they won't be doing anything besides GET requests. If that's the case, I can update my answer. – rmarscher Feb 06 '15 at 04:04
  • Redirect is still on the same domain. I'm creating a one step install for LMS, the location where all the requests are made determines what system it is and what page its meant to be on. Instead of the url always being the same such as `domain.com/setup` I'd like it to route to the correct controller/ method instead. – Bankzilla Feb 08 '15 at 19:59

1 Answers1

0

From within a controller:

$this->redirect(['AnotherController::anotherAction', '?' => $this->request->query], ['exit' => true]);

$this->request->data can't be forwarded. Browser location redirects can only specify the query string. You could maybe render a page with a form that contains the data in input html elements and submit the form on load. Or you could stash the data in a session or somewhere server side and retrieve it on the next request.

rmarscher
  • 5,596
  • 2
  • 28
  • 30