Framework integration / Symfony / Getting started

The SearchService is your entry point for any operation with the Algolia engine:

  • Index data
  • Search
  • Clear data

Retrieving the SearchService

Type-hinting

Symfony 4 ships with a lighter container, where only core services are registered. If your controller will be responsible for search-related tasks, you can inject the SearchService into the constructor by type-hinting the SearchServiceInterface. Symfony will take care of instantiating the right implementation for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 easily get the search.service from the container.

Be aware however that it’s not considered a best practice by Symfony. We recommend using type-hinting to retrieve the SearchService.

1
2
3
4
5
6
// In a container aware class
$searchService = $this->getContainer()->get('search.service');

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

Did you find this page helpful?