Skip to main content
Use the SearchService to index, search, and manage data with Algolia.

Inject the SearchService using type hints

Symfony 4 ships with a lighter container that registers only core services. For search tasks, inject the SearchService into the constructor. Symfony takes care of instantiating the right implementation for you.
PHP
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Algolia\SearchBundle\SearchService;

class ExampleController extends AbstractController
{
    protected $searchService;

    public function __construct(SearchServiceInterface $searchService)
    {
        $this->searchService = $searchService;
    }
}

Access the SearchService from the container

Prefer injecting the SearchServiceInterface with type hints over retrieving it from the container. This approach follows Symfony best practices and improves testability.
Symfony 3 uses a container holding all public services, and services are public by default. This way, you can get the search.service from the container.
PHP
// In a container aware class
$searchService = $this->getContainer()->get('search.service');

// In a Symfony\Bundle\FrameworkBundle\Controller\Controller class
$searchService = $this->get('search.service');