Slim 3 - redirect to a route with GET parameters

I have to do something wrong.

I need a route that parses and combines an array of parameters GETto redirect to another route waiting for GETparameters.

I was hoping this would work when I pass $search_paramsas part of the method pathFor():

// 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);

});

But this did not add the array $search_paramsas GET parameters. I understand that if a route was /searchexpecting arguments in a URL with something like {q}, it would get caught, but I need to add an unknown group of parameters GET.

My workaround is to do the following, manually using http_build_query()to add the parameters GETas 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 ?

POST GET? HTTP- 307 withStatus() , - , /search, .

.

0
1

q -param , 3 :

q - , , - /search/{q},

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

All Articles