Improved developer experience with the Algolia PHP client version 2
With the version upgrade, you can benefit from these new capabilities of the PHP API client:
Copy a whole
Copy all rules from an index
Replace all data in an index
Generate secured API keys
For example:
PHP
Report incorrect code
Copy
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;class ArticleController extends AbstractController{ public function refillAction(SearchClient $client, array $objects) { // Copy an index $client->copyIndex('SRC_INDEX_NAME', 'DEST_INDEX_NAME'); // Copy all the rules from an index $client->copyRules('SRC_INDEX_NAME', 'DEST_INDEX_NAME'); // Replace all objects $index = $client->initIndex('DEST_INDEX_NAME'); $index->replaceAllObjects($objects); }}
IndexManager has been renamed to SearchService.
This is the only service you need to interact with Algolia.
PHP
Report incorrect code
Copy
namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;- use Algolia\SearchBundle\IndexManagerInterface;+ use Algolia\SearchBundle\SearchService; class ExampleController extends Controller {- protected $indexManager;+ protected $searchService;- public function __construct(IndexManagerInterface $indexingManager)+ public function __construct(SearchServiceInterface $searchService) {- $this->indexManager = $indexingManager;+ $this->searchService = $searchService; } }
Methods from the SearchService are now waitable.
You can use the wait() method to wait for your task to be completely handled by the engine before moving on, instead of handling this logic yourself.Examples:
PHP
Report incorrect code
Copy
// index objects and wait for the task to finish$response = $this->searchService->index($entityManager, $objects)->wait();// clear an index$response = $this->searchService->clear($className)->wait();
This also means that the return type of waitable methods has changed. They now all return an AbstractResponse. Update your code accordingly.
Optional arguments are passed in an array or as RequestOptions object as the last argument
The client never sets any default values
You can pass any optional arguments and search parameters to the engine in the $requestOptions parameter, available in all SearchService methods. For example, you can add filters, choose which facets to retrieve, change the number of hits per page, or pass new headers.For example:
PHP
Report incorrect code
Copy
- $result = $this->indexManager->remove(+ $result = $this->searchService->remove(+ $this->get('doctrine')->getManager(), $searchablePosts,- $this->get('doctrine')->getManager()- // you could not pass requestOptions+ // here you can pass any requestOptions or headers, for example 'X-Forwarded-For', 'X-Algolia-UserToken'...+ [+ 'X-Forwarded-For' => '0.0.0.0',+ ]+ );
PHP
Report incorrect code
Copy
- $result = $this->indexManager->search(+ $result = $this->searchService->search(+ $this->get('doctrine')->getManager(),+ Post::class, 'foo',- Post::class,- $this->get('doctrine')->getManager(),- // the optional page and hitsPerPage parameters were passed separately- // page argument was starting from 1- 1,- 20,- 'attributesToRetrieve' => [- 'title',- ]+ // all the optional parameters are now sent as once in the $requestOptions+ // be careful as page argument now starts from 0+ [+ 'page' => 0,+ 'hitsPerPage' => 20,+ 'attributesToRetrieve' => [+ 'title',+ ],+ ]+ );
For testing, use Algolia\SearchBundle\SearchService by mocking or extending it,
and override the search.service in your test configuration.
For more information, see: