我第一次使用Slim框架,到目前为止,所有内容都是正确的。但有一件事我似乎无法理解。发布表单后,我想重定向回同一页面,但它在网址中使用了一个参数,我无法回复它。这就是我到目前为止所做的:
$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');
非常感谢任何帮助。感谢
答案 0 :(得分:5)
urlFor方法接受两个参数:
试试这个:
http://yoursite.com/News/123/some-news-article
答案 1 :(得分:5)
对于那些寻找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
有关苗条2到苗条3的差异的任何其他信息,请参阅Slim Upgrade Guide
答案 2 :(得分:2)
Slim 3解决方案的另一个答案不是很优美,并且错过了以下事实: 一个空数组必须作为第二个参数传递 作为第三个参数;没有这个,no query parameters are used。
因此,Slim 3的答案很简单。
return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
'key1' => $value1,
'key2' => $value2
]));