Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim Framework redirect with params

Tags:

redirect

php

slim

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

like image 442
Adam Avatar asked Jun 21 '15 22:06

Adam


1 Answers

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

like image 82
James McClelland Avatar answered Sep 27 '22 18:09

James McClelland