I'd like a route that parses and puts together an array of GET
parameters to redirect to another route that expects GET
parameters.
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);
});
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.
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);
});
But that feels clunky. Am I missing something about Slim 3 and redirects?
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.
You want to add the
q
-param inside the query, the router has 3 parameter: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