5

I'm using Slim framework for the first time, and so far everything is spot on. But there is one thing I can't seem to get my head around. After posting a form I would like to redirect back to the same page, but it uses a param in the url and I can't get back to it. This is what I have so far:

$app->post('/markets-:game', $authenticated(), function($game) use ($app) {

    $request = $app->request();

    $id = $request->post('game3');

    $app->flash('global', 'game added');
    $app->response->redirect($app->urlFor('games.markets', {"game:$id"}));

})->name('games.markets.post');

Any help would be much appreciated. Thanks

alexw
  • 8,468
  • 6
  • 54
  • 86
Adam
  • 455
  • 1
  • 3
  • 18
  • For Slim v3.x an answer with enough details: [#38939583](http://stackoverflow.com/questions/23404355/how-to-use-slim-redirect/38939583#38939583) – MNR Aug 14 '16 at 06:19

3 Answers3

7

For anyone who lands here looking for a Slim 3 solution, the information is documented in the upgrade guide.

To redirect to a route with parameters is now as follows:

$url = $this->router->pathFor('games.markets', ['game' => $id]);
return $response->withStatus(302)->withHeader('Location', $url);

On another note, when naming routes you must now use

$app->get('/', function (Request $request, Response $response) {...})->setName('route.name');

Rather than the old ->name

Any other information regarding the differences from slim 2 to slim 3 can be found on the Slim Upgrade Guide

James McClelland
  • 555
  • 4
  • 16
  • The parameter 'game' need to be received in the redirected route games.market? – Biguá Oct 06 '16 at 22:09
  • In this example yes but you can just remove the array to redirect to the route without params – James McClelland Oct 10 '16 at 08:38
  • 1
    Beware: if your route is expecting ($request, $response, $args), you might have to pass '[]' in the middle. Re: this helpful answer: http://stackoverflow.com/a/41705732/1196358 – ghukill Jan 20 '17 at 18:13
6

urlFor method accepts two parameters:

  1. the name of the route;
  2. an associative array with all the parameters (optional).

Try this:

$app->response->redirect($app->urlFor('games.markets', array('game' => $id)));
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
6

Slim 3 Solution

The other answer for a Slim 3 solution is not very graceful and also misses the fact that an empty array must be passed as a second parameter to pass the query parameters as a third parameter; without this, no query parameters are used.

Therefore, the answer for Slim 3 would simply be the following.

return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
    'key1' => $value1,
    'key2' => $value2
]));
Alex W
  • 321
  • 4
  • 12
  • Is it possible to send it through as a post method ? so it doesn't show in the browser ? – iamafasha Oct 26 '19 at 08:26
  • 1
    @novapack That is not possible in as simple a manner as you would hope, but that is a choice of the HTTP protocol, not Slim. It is possible only with an intermediate page, which you can redirect them to in the usual way (e.g. above) which then makes the POST request. See the top answer here: https://stackoverflow.com/a/5576700/5233676 – Alex W Oct 30 '19 at 20:56