使用GET参数重定向到路由 [英] Redirect to route with GET parameters

查看:206
本文介绍了使用GET参数重定向到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一条解析并组合了 GET 参数数组的路由,以重定向到期望 GET 参数。

I'd like a route that parses and puts together an array of GET parameters to redirect to another route that expects GET parameters.

我希望这能行得通,在这里我将 $ search_params 作为 pathFor()方法:

I had hoped this would work, where I pass $search_params as part of the pathFor() method:

// SEARCH VIEW
$app->get('/search', function ($request, $response, $args) {
    $api = $this->APIRequest->get($request->getAttribute('path'),$request->getQueryParams());
    $args['data'] = json_decode($api->getBody(), true);
    return $this->view->render($response, 'search.html.twig', $args);
})->setName('search');

// ADVANCED SEARCH VIEW
$app->get('/advanced_search', function ($request, $response, $args) {    
    return $this->view->render($response, 'advanced_search.html.twig', $args);
});

// ADVANCED SEARCH PROCESS
$app->post('/advanced_search', function ($request, $response, $args) {    

    // get settings
    $settings = $this->get('settings');

    // get post parameters
    $qp = $request->getParsedBody();

    // translate advanced search form parameters to Solr-ese
    $search_params = array();
    $search_params['q'] = $qp['query'];

    // redirect to GET:/search, with search parameters
    $url = $this->router->pathFor('search', $search_params);    
    return $response->withStatus(302)->withHeader('Location', $url);

});

但这没有追加数组 $ search_params 作为GET参数。我了解,如果 / search 路由在URL中带有预期的参数,例如 {q} ,它将被捕获,但我需要附加一堆未知的 GET 参数。

But this did not append the array $search_params as GET parameters. I understand that if the /search route had expected arguments in the URL with something like {q} it would get caught, but I need to append an unknown bunch of GET parameters.

我的解决方法是手动执行以下操作使用 http_build_query() GET 参数作为字符串附加到路由URL:

My workaround is to do the following, manually using http_build_query() to append the GET parameters as a string to the route URL:

// SEARCH VIEW
$app->get('/search', function ($request, $response, $args) {
    $api = $this->APIRequest->get($request->getAttribute('path'),$request->getQueryParams());
    $args['data'] = json_decode($api->getBody(), true);
    return $this->view->render($response, 'search.html.twig', $args);
})->setName('search');

// ADVANCED SEARCH VIEW
$app->get('/advanced_search', function ($request, $response, $args) {    
    return $this->view->render($response, 'advanced_search.html.twig', $args);
});

// ADVANCED SEARCH PROCESS
$app->post('/advanced_search', function ($request, $response, $args) {    

    // get settings
    $settings = $this->get('settings');

    // get post parameters
    $qp = $request->getParsedBody();

    // translate advanced search form parameters to Solr-ese
    $search_params = array();
    $search_params['q'] = $qp['query'];

    // redirect to GET:/search, with search parameters
    $url = $this->router->pathFor('search')."?".http_build_query($search_params);    
    return $response->withStatus(302)->withHeader('Location', $url);

});

但这感觉很笨拙。我是否缺少有关Slim 3和重定向的信息?

But that feels clunky. Am I missing something about Slim 3 and redirects?

它与 POST 路由重定向到 GET 路线?我尝试在重定向中的 withStatus()中使用HTTP代码 307 ,但正如预期的那样,更改了方法请求转到 / search ,这对于我们的目的不起作用。

Is it related to a POST route redirecting to a GET route? I tried using the HTTP code 307 for the withStatus() in the redirect, but as somewhat expected, that changed the method request going to /search, which doesn't work for our purposes.

推荐答案

您要在查询中添加 q 参数,路由器具有3个参数:

You want to add the q-param inside the query, the router has 3 parameter:


  1. 路由名称

  2. 路由模式占位符和替换值的关联数组

  3. 查询参数的关联数组

您当前正在添加 q 参数作为路由占位符,如果您有像这样的路由 / search / {q} ,因此要将其添加为查询参数,请使用第3个参数

You are currently adding your q-parameter as route placeholder, that would work if you have something like this as route /search/{q} so for adding it as query parameter, use the 3rd parameter

$url = $this->router->pathFor('search', [], $search_params);

这篇关于使用GET参数重定向到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆