Skip to main content
The SearchService is your entry point for any operation with the Algolia engine:
  • Index data
  • Search
  • Clear data

Retrieve the SearchService

Type-hinting

Symfony 4 ships with a lighter container, where only core services are registered. If your controller is responsible for search-related tasks, you can inject the SearchService into the constructor by type-hinting the SearchServiceInterface. 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;
    }
}

Directly from the container

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. Note, that Symfony doesn’t consider this a best practice. Instead, use type-hinting to retrieve the SearchService.
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');
⌘I