我是第一次使用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');
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.谢谢
对于那些寻找Slim 3解决方案的人来说,这些信息都记录在升级指南中.
要重定向到带参数的路由,现在如下所示:
$url = $this->router->pathFor('games.markets', ['game' => $id]);
return $response->withStatus(302)->withHeader('Location', $url);
Run Code Online (Sandbox Code Playgroud)
另一方面,在命名路线时,您现在必须使用
$app->get('/', function (Request $request, Response $response) {...})->setName('route.name');
Run Code Online (Sandbox Code Playgroud)
而不是旧的 ->name
有关从slim 2到slim 3的差异的任何其他信息,请参阅Slim Upgrade Guide
urlFor方法接受两个参数:
试试这个:
$app->response->redirect($app->urlFor('games.markets', array('game' => $id)));
Run Code Online (Sandbox Code Playgroud)
Slim 3 解决方案的另一个答案不是很优雅,并且还忽略了一个事实,即必须将空数组作为第二个参数传递才能将查询参数作为第三个参数传递;没有这个,就不会使用查询参数。
因此,Slim 3 的答案如下。
return $response->withRedirect($this->router->pathFor('named.path.of.route', [], [
'key1' => $value1,
'key2' => $value2
]));
Run Code Online (Sandbox Code Playgroud)