Working with the Algolia InstantSearch js library and the PHP client, I wish to pass the query string created by InstantSearch urlSync into the PHP library's search method. Is there a convenient way to do this or will I have to translate the query string to the params the library expects by myself?
For example, I have the string:
?q=&hPP=1000&idx=myindex&p=0&dFR%5Bcolor%5D%5B0%5D=orange&is_v=1
Ideally I would like to
$client = new \AlgoliaSearch\Client("x", "x");
$index = $client->initIndex($_GET['idx']);
parse_str($_SERVER['QUERY_STRING'], $params);
$res = $index->search('', $params);
but doing so throws an error that there are unexpected properties in the params passed.
Unfortunately, it's indeed not possible to reuse the instant search URL in your back-end using Algolia clients. I would assume you're asking this for SEO purposes, which is a definitely valid point.
The only language in which this would be doable at the moment is JavaScript. Indeed, this logic is extractable from the underlying library of instantsearch.js
: algoliasearch-helper-js
.
getStateFromQueryString
to get the state setState
getQuery
method You could actually add this logic in the front-end, URL-encode this query and set it in a custom parameter that you'd decode in your back-end. That's pretty ugly but it could work (it might not fit in the maximum URI length though).
In the front-end, you wouldn't have to go through the process of extracting the state from the URL and setting the state to a new helper, you can simply use search.helper
- where search
is your instantsearch.js
instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.