带有参数的 Slim Framework 重定向
Slim Framework redirect with params
我是第一次使用 Slim 框架,到目前为止一切都很好。但是有一件事我似乎无法理解。发布表单后,我想重定向回同一页面,但它在 url 中使用了一个参数,我无法返回。这是我目前所拥有的:
$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');
如有任何帮助,我们将不胜感激。谢谢
urlFor 方法接受两个参数:
- 路线名称;
- 一个包含所有参数的关联数组(可选)。
试试这个:
$app->response->redirect($app->urlFor('games.markets', array('game' => $id)));
对于来到这里寻找 Slim 3 解决方案的任何人,信息都记录在升级指南中。
现在重定向到带参数的路由如下:
$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
修身 3 解决方案
Slim 3 解决方案的另一个答案不是很优雅,也忽略了 必须将空数组作为第二个参数传递的事实 将查询参数作为第三个参数传递;没有这个,no query parameters are used.
因此,Slim 3 的答案如下。
return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
'key1' => $value1,
'key2' => $value2
]));
我是第一次使用 Slim 框架,到目前为止一切都很好。但是有一件事我似乎无法理解。发布表单后,我想重定向回同一页面,但它在 url 中使用了一个参数,我无法返回。这是我目前所拥有的:
$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');
如有任何帮助,我们将不胜感激。谢谢
urlFor 方法接受两个参数:
- 路线名称;
- 一个包含所有参数的关联数组(可选)。
试试这个:
$app->response->redirect($app->urlFor('games.markets', array('game' => $id)));
对于来到这里寻找 Slim 3 解决方案的任何人,信息都记录在升级指南中。
现在重定向到带参数的路由如下:
$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
修身 3 解决方案
Slim 3 解决方案的另一个答案不是很优雅,也忽略了 必须将空数组作为第二个参数传递的事实 将查询参数作为第三个参数传递;没有这个,no query parameters are used.
因此,Slim 3 的答案如下。
return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
'key1' => $value1,
'key2' => $value2
]));