简体   繁体   English

Slim Framework重定向与params

[英]Slim Framework redirect with params

I'm using Slim framework for the first time, and so far everything is spot on. 我是第一次使用Slim框架,到目前为止一切都是现货。 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 谢谢

urlFor method accepts two parameters: urlFor方法接受两个参数:

  1. the name of the route; 路线的名称;
  2. an associative array with all the parameters (optional). 包含所有参数的关联数组(可选)。

Try this: 试试这个:

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

For anyone who lands here looking for a Slim 3 solution, the information is documented in the upgrade guide. 对于那些寻找Slim 3解决方案的人来说,这些信息都记录在升级指南中。

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 而不是旧的->name

Any other information regarding the differences from slim 2 to slim 3 can be found on the Slim Upgrade Guide 有关从slim 2到slim 3的差异的任何其他信息,请参阅Slim Upgrade Guide

Slim 3 Solution 超薄3解决方案

The other answer for a Slim 3 solution is not very graceful and also misses the fact that an empty array must be passed as a second parameter to pass the query parameters as a third parameter; Slim 3解决方案的另一个答案并不是非常优雅,并且还错过了必须将空数组作为第二个参数传递以将查询参数作为第三个参数传递的事实; without this, no query parameters are used . 没有它, 不使用任何查询参数

Therefore, the answer for Slim 3 would simply be the following. 因此,Slim 3的答案就是以下几点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM