Redirect Slim Framework with options

This is the first time I'm using Slim framework, and everything is still happening. But there is one thing that I cannot say about. After the form is published, I would like to redirect back to the same page, but it uses the parameter in the URL, and I cannot return to it. This is what I still have:

$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 greatly appreciated. Thanks

+4
source share
3 answers

The urlFor method takes two parameters:

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

Try it:

$app->response->redirect($app->urlFor('games.markets', array('game' => $id)));
+5
source

For those who land here in search of a Slim 3 solution, the information is documented in the upgrade guide.

:

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

, ,

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

->name

slim 2 slim 3 Slim Upgrade Guide

+5

Slim 3 solution

Another answer for Slim 3's solution is not very elegant and also does not take into account the fact that an empty array must be passed as a second parameter in order to pass request parameters as a third parameter; Without this , query parameters are not used .

Therefore, the answer for Slim 3 is simply the following.

return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
    'key1' => $value1,
    'key2' => $value2
]));
0
source

All Articles