evilfreelancer / laravel-manticoresearch
An easy way to use the official ManticoreSearch client in your Laravel applications
Fund package maintenance!
pay.cloudtips.ru/p/937f48ac
boosty.to/evilfreelancer
Installs: 1 028
Dependents: 0
Suggesters: 0
Security: 0
Stars: 23
Watchers: 4
Forks: 7
Open Issues: 0
Requires
- php: ^7.2|^8.0
- ext-json: *
- illuminate/contracts: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- manticoresoftware/manticoresearch-php: ^1.6
- psr/log: >=1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
- roave/security-advisories: dev-master
Suggests
- ext-curl: *
- monolog/monolog: *
README
Laravel ManticoreSearch plugin
An easiest way to use the official ManticoreSearch client in your Laravel or Lumen applications.
composer require evilfreelancer/laravel-manticoresearch
Post install
Laravel
The package's service provider will automatically register its service provider.
Publish the configuration file:
php artisan vendor:publish --provider="ManticoreSearch\Laravel\ServiceProvider"
Alternative configuration method via .env file
After you publish the configuration file as suggested above, you may configure ManticoreSearch by adding the following
to your application's .env
file (with appropriate values):
MANTICORESEARCH_HOST=localhost MANTICORESEARCH_PORT=9200 MANTICORESEARCH_TRANSPORT=Http MANTICORESEARCH_USER= MANTICORESEARCH_PASS=
All available environments variables
Lumen
If you work with Lumen, please register the service provider and configuration in bootstrap/app.php
:
// Enable shortname of facade $app->withFacades(true, [ 'ManticoreSearch\Laravel\Facade' => 'Facade', ]); // Register Config Files $app->configure('manticoresearch'); // Register Service Providers $app->register(ManticoreSearch\Laravel\ServiceProvider::class);
Manually copy the configuration file to your application.
How to use
The ManticoreSearch
facade is just an entry point into
the ManticoreSearch client, so previously you might have
used:
require_once __DIR__ . '/vendor/autoload.php'; $config = ['host'=>'127.0.0.1', 'port'=>9308]; $client = new \Manticoresearch\Client($config); $index = new \Manticoresearch\Index($client); $index->setName('movies');
Instead of these few lines above you can use single line solution:
$index = \ManticoreSearch::index('movies');
That will run the command on the default connection. You can run a command on any connection (see the defaultConnection setting and connections array in the configuration file).
$index = \ManticoreSearch::connection('connectionName')->index($nameOfIndex); $pq = \ManticoreSearch::connection('connectionName')->pq(); $cluster = \ManticoreSearch::connection('connectionName')->cluster(); $indices = \ManticoreSearch::connection('connectionName')->indices(); $nodes = \ManticoreSearch::connection('connectionName')->nodes(); // etc...
methods of the Client class:
\ManticoreSearch::connection('connectionName')->sql($params); \ManticoreSearch::connection('connectionName')->replace($params); \ManticoreSearch::connection('connectionName')->delete($params); // etc...
Lumen users who aren't using facades will need to use dependency injection, or the application container in order to get the ManticoreSearch Index object:
// using injection: public function handle(\ManticoreSearch\Laravel\Manager $manticoresearch) { $manticoresearch->describe(); } // using application container: $manticoreSearch = $this->app('manticoresearch');
Of course, dependency injection and the application container work for Laravel applications as well.
Logging
Since the PHP client of the ManticoreSearch supports logging through PSR-compatible loggers, you can use them in the same way as presented in the official documentation.
For example, you want to use the Monolog logger.
composer require monolog/monolog
By default, you need to write something like this:
$logger = new \Monolog\Logger('name'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO)); $config = ['host' => '127.0.0.1', 'port' => 9306]; $client = new \Manticoresearch\Client($config, $logger); $index = new \Manticoresearch\Index($client); $index->setName('movies');
But if you want to use the Monolog together with this library then you may simplify your code like this:
$logger = new \Monolog\Logger('name'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO)); $index = \ManticoreSearch::connection('connectionName', $logger)->index('movies');
Testing
Just install dev requirements composer install --dev
, then execute following command from root of this library:
./vendor/bin/phpunit