ermakove / laravel-manticoresearch
在 Laravel 应用程序中使用官方 ManticoreSearch 客户端的一种简单方法
Requires
- php: ^7.2|^8.0
- ext-json: *
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.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: *
This package is not auto-updated.
Last update: 2024-09-24 22:25:22 UTC
README
Laravel ManticoreSearch 插件
在 Laravel 或 Lumen 应用程序中使用 官方 ManticoreSearch 客户端 的一种简单方法。
composer require evilfreelancer/laravel-manticoresearch
安装后
Laravel
该包的服务提供程序将自动注册其服务提供程序。
发布配置文件
php artisan vendor:publish --provider="ManticoreSearch\Laravel\ServiceProvider"
通过 .env 文件进行替代配置方法
按照上述建议发布配置文件后,您可以通过向应用程序的 .env 文件中添加以下内容来配置 ManticoreSearch(使用适当的值)
MANTICORESEARCH_HOST=localhost MANTICORESEARCH_PORT=9200 MANTICORESEARCH_TRANSPORT=Http MANTICORESEARCH_USER= MANTICORESEARCH_PASS=
所有可用环境变量
Lumen
如果您使用 Lumen,请在 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);
手动将配置文件复制到您的应用程序中。
如何使用
ManticoreSearch 门面只是进入 ManticoreSearch 客户端 的一个入口点,因此您可能之前使用过以下几行代码
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');
您可以使用以下单行解决方案代替上述几行代码
$index = \ManticoreSearch::index('movies');
这将在默认连接上运行命令。您可以在任何连接上运行命令(请参阅配置文件中的 defaultConnection 设置和 connections 数组)。
$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...
客户端类的方法
\ManticoreSearch::connection('connectionName')->sql($params); \ManticoreSearch::connection('connectionName')->replace($params); \ManticoreSearch::connection('connectionName')->delete($params); // etc...
未使用门面的 Lumen 用户需要使用依赖注入或应用程序容器来获取 ManticoreSearch 索引对象
// using injection: public function handle(\ManticoreSearch\Laravel\Manager $manticoresearch) { $manticoresearch->describe(); } // using application container: $manticoreSearch = $this->app('manticoresearch');
当然,依赖注入和应用程序容器也适用于 Laravel 应用程序。
日志记录
由于 ManticoreSearch 的 PHP 客户端支持通过 PSR 兼容的日志记录器进行日志记录,因此您可以像在 官方文档 中展示的那样使用它们。
例如,您想使用 Monolog 日志记录器。
composer require monolog/monolog
默认情况下,您需要编写类似以下内容
$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');
但如果您想同时使用 Monolog 和此库,则可以简化您的代码如下
$logger = new \Monolog\Logger('name'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO)); $index = \ManticoreSearch::connection('connectionName', $logger)->index('movies');
测试
只需安装开发需求 composer install --dev,然后从该库的根目录执行以下命令
./vendor/bin/phpunit