PHP code example of tacman / ux-search

1. Go to this page and download the library: Download tacman/ux-search 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/ */

    

tacman / ux-search example snippets


// config/bundles.php
return [
    // ...
    Mezcalito\UxSearchBundle\MezcalitoUxSearchBundle::class => ['all' => true],
];

use Mezcalito\UxSearchBundle\Search\AbstractSearch;
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
use Mezcalito\UxSearchBundle\Twig\Components\Facet\RangeInput;

#[AsSearch(index: 'products', adapter: 'default')]
class ProductSearch extends AbstractSearch
{
    public function build(array $options = []): void
    {
        // Add facets for filtering
        $this->addFacet('brand', 'Brand');
        $this->addFacet('category', 'Category');
        $this->addFacet('price', 'Price', RangeInput::class);

        // Add sorting options
        $this->addAvailableSort('name', 'Name');
        $this->addAvailableSort('price', 'Price');
        $this->addAvailableSort('created_at', 'Newest');

        // Configure pagination
        $this->setAvailableHitsPerPage([12, 24, 48]);

        // Adapter-specific parameters
        $this->setAdapterParameters([
            // Adapter-specific options here
        ]);
    }
}

use Mezcalito\UxSearchBundle\Event\PreSearchEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SearchSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            PreSearchEvent::class => 'onPreSearch',
        ];
    }

    public function onPreSearch(PreSearchEvent $event): void
    {
        $query = $event->getQuery();
        // Modify the query before search execution
        $query->addFilter('status', 'published');
    }
}

// Product search with Algolia
#[AsSearch(index: 'products', adapter: 'algolia')]
class ProductSearch extends AbstractSearch { }

// Blog search with Meilisearch
#[AsSearch(index: 'posts', adapter: 'meilisearch')]
class BlogSearch extends AbstractSearch { }

// User search with Doctrine
#[AsSearch(index: 'App\Entity\User', adapter: 'orm')]
class UserSearch extends AbstractSearch { }