PHP code example of ics / search-bundle

1. Go to this page and download the library: Download ics/search-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ics / search-bundle example snippets


// config/bundles.php

return [
    // ...
    ICS\SearchBundle\SearchBundle::class => ['all' => true],
];

# src/Entity/User.php

use ICS\SearchBundle\Entity\EntitySearchInterface;

class User implements EntitySearchInterface
{
    // Define name show in search results interface
    public static function getEntityClearName(): string
    {
        return "Application User";
    }
    // Define Template of the results
    public static function getSearchTwigTemplate(): string
    {
        return "search/result.html.twig";
    }
    // Define ROLES have access to the results
    public static function getRolesSearchEnabled(): array
    {
        return [
            'ROLE_ADMIN'
        ];
    }

}


# src/Repository/UserRepository.php

use ICS\SearchBundle\Entity\EntitySearchRepositoryInterface;

class UserRepository extends ServiceEntityRepository implements EntitySearchRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }
    
    public function search(string $search) : ?Collection
    {
        $results = $this->createQueryBuilder('u')
            ->where('lower(u.name) LIKE lower(:search)')
            ->orWhere('lower(u.surname) LIKE lower(:search)')
            ->setParameter('search', "%".$search."%")
            ->orderBy('u.name', 'ASC')
            ->getQuery()
            ->getResult();
        return new ArrayCollection($results);
    }
  
}