Simplify example
This commit is contained in:
@@ -61,21 +61,18 @@ class Company implements SearchableInterface
|
|||||||
Query the index from a controller and resolve each hit to a URL:
|
Query the index from a controller and resolve each hit to a URL:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
#[Route('/search', name: 'app_search', methods: ['GET'])]
|
#[Route('/search', name: 'app_search')]
|
||||||
public function search(Request $request, SearchIndexRepository $repo): Response
|
public function search(Request $request, SearchIndexRepository $repo): Response
|
||||||
{
|
{
|
||||||
$routes = [Company::class => 'app_company_show'];
|
$routes = [Company::class => 'app_company_show'];
|
||||||
|
|
||||||
$rows = $repo->findAllPagination($request->query->get('q', ''))->getResult();
|
$rows = $repo->findAllPagination($request->query->get('q', ''))->getResult();
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
foreach ($rows as [$index]) {
|
foreach ($rows as [$index]) {
|
||||||
$route = $routes[$index->getEntityClass()] ?? null;
|
|
||||||
if ($route === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$results[] = [
|
$results[] = [
|
||||||
'title' => $index->getTitle(),
|
'title' => $index->getTitle(),
|
||||||
'url' => $this->generateUrl($route, ['id' => $index->getEntityId()]),
|
'url' => $this->generateUrl($routes[$index->getEntityClass()], ['id' => $index->getEntityId()]),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user