简体   繁体   English

使用Algolia,如何将urlSync创建的查询字符串传递到php客户端?

[英]With Algolia, how to pass the query string created by urlSync into the php client?

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. 使用Algolia InstantSearch js库和PHP客户端,我希望将InstantSearch urlSync创建的查询字符串传递到PHP库的搜索方法中。 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. 不幸的是,实际上不可能使用Algolia客户端在后端重新使用即时搜索URL。 I would assume you're asking this for SEO purposes, which is a definitely valid point. 我认为您是出于SEO的目的而问这个,这绝对是正确的一点。

The only language in which this would be doable at the moment is JavaScript. 目前唯一可行的语言是JavaScript。 Indeed, this logic is extractable from the underlying library of instantsearch.js : algoliasearch-helper-js . 实际上,该逻辑是从底层库可提取instantsearch.jsalgoliasearch-helper-js

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. 您实际上可以在前端添加此逻辑,对该查询进行URL编码,然后将其设置为在后端解码的自定义参数。 That's pretty ugly but it could work (it might not fit in the maximum URI length though). 这很丑陋,但可以工作(尽管可能不适合最大URI长度)。

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. 在前端,您无需执行从URL提取状态并将状态设置为新帮助程序的过程,只需使用search.helper search就是您的instantsearch.js实例。

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

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