Installation
composer require agentsib/sphinx-bundle
Ensure your composer.json targets Symfony 2.3+ (as per requirements).
Configuration
Add the bundle to AppKernel.php:
new Agentsib\SphinxBundle\AgentsibSphinxBundle(),
Configure Sphinx connection in config.yml:
agentsib_sphinx:
connections:
default:
host: '127.0.0.1'
port: 9306
index: 'your_index_name'
weight: 1000
First Query
Inject the SphinxManager service and run a basic query:
use Agentsib\SphinxBundle\Manager\SphinxManager;
class SearchController extends Controller
{
public function search(SphinxManager $sphinx)
{
$results = $sphinx->query('your_search_term');
return $this->render('search/results.html.twig', ['results' => $results]);
}
}
Resources/config/services.yml (Service definitions)DependencyInjection/Configuration.php (Bundle configuration schema)Manager/SphinxManager.php (Core query logic)Leverage foolz/sphinxql-query-builder for complex queries:
$query = $sphinx->createQueryBuilder()
->select('id, title, content')
->from('your_index')
->where('MATCH(@title)')->equals('search_term')
->limit(10);
$results = $sphinx->query($query);
Bind search results to form fields dynamically:
$form = $this->createFormBuilder()
->add('query', TextType::class, [
'data' => $request->query->get('q'),
])
->getForm();
Use KnpPaginatorBundle alongside Sphinx:
use Knp\Component\Pager\PaginatorInterface;
public function paginatedSearch(SphinxManager $sphinx, PaginatorInterface $paginator)
{
$results = $sphinx->query('term', 10, ($page-1)*10);
$pagination = $paginator->paginate(
$results,
$page,
10
);
}
Cache frequent queries with Symfony Cache component:
$cache = $this->get('cache.app');
$cacheKey = 'sphinx_results_' . md5('term');
$results = $cache->get($cacheKey, function() use ($sphinx) {
return $sphinx->query('term');
});
Index Mismatch
Ensure the configured index in config.yml matches your Sphinx index name. Test with:
sphinxql -h 127.0.0.1 -P 9306 -Q "SHOW INDEXES"
Query Builder Syntax
The foolz/sphinxql-query-builder may differ from native SphinxQL. Validate queries with:
$query->getSQL(); // Outputs raw SphinxQL
Dependency Conflicts Symfony 2.3+ is required. Avoid mixing with newer Symfony versions without patching.
Enable Sphinx Logging
Add to config.yml:
agentsib_sphinx:
debug: true
Logs appear in var/log/dev.log.
Query Profiling Use XHProf or Sphinx’s built-in profiling:
$sphinx->setDebug(true);
$results = $sphinx->query('term');
Custom Query Handlers
Extend SphinxManager to add methods for domain-specific queries:
class CustomSphinxManager extends SphinxManager
{
public function searchProducts($term)
{
return $this->query("SELECT * FROM products WHERE MATCH('@title') = '$term'");
}
}
Event Listeners
Subscribe to agentsib_sphinx.query events for pre/post-query logic:
services:
app.sphinx_listener:
class: AppBundle\EventListener\SphinxListener
tags:
- { name: kernel.event_listener, event: agentsib_sphinx.query, method: onQuery }
Async Search Use Symfony Messenger to offload heavy queries:
$message = new SphinxSearchMessage('term');
$this->get('messenger')->dispatch($message);
weight in config.yml to balance Sphinx vs. other search backends (e.g., Elasticsearch).connections:
primary:
host: '127.0.0.1'
backup:
host: '10.0.0.1'
weight: 100
How can I help you explore Laravel packages today?