miroshnichenko-yaroslav / laravel-algolia
Laravel 的 Algolia 桥接器
Requires
- php: ^7.0
- algolia/algoliasearch-client-php: ^1.13
- graham-campbell/manager: ^2.5
- illuminate/contracts: 5.3.* || 5.4.*
- illuminate/support: 5.3.* || 5.4.*
Requires (Dev)
- graham-campbell/testbench: ^3.3
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.7
README
Laravel 的 Algolia 桥接器。
// Search. $algolia->search('marty mcfly'); // Create global API keys. $algolia->addUserKey(['search'], 300); // Want to use the facade? Algolia::getLogs();
安装
使用 Composer 在项目的根目录中安装此包。
$ composer require vinkla/algolia
将服务提供者添加到 config/app.php
中的 providers
数组。
Vinkla\Algolia\AlgoliaServiceProvider::class
如果您想使用 facade,可以在 config/app.php
中添加引用到您的别名数组。
'Algolia' => Vinkla\Algolia\Facades\Algolia::class
配置
Laravel Algolia 需要连接配置。要开始,您需要发布所有供应商资产
$ php artisan vendor:publish
这将在您的应用程序中创建一个 config/algolia.php
文件,您可以根据需要进行修改以设置配置。同时,请确保检查此包中原始配置文件之间的更改。
默认连接名称
此选项 default
是您可能指定要使用以下哪个连接作为您所有工作的默认连接的地方。当然,您可以使用管理器类同时使用多个连接。此设置的默认值为 main
。
Algolia 连接
此选项 connections
是为您的应用程序设置每个连接的地方。已包含示例配置,但您可以根据需要添加任意多的连接。
用法
AlgoliaManager
这是最感兴趣的类。它绑定到 ioc 容器中的 algolia
,并可以使用 Facades\Algolia
facade 访问。此类通过扩展 AbstractManager 实现 ManagerInterface。该接口和抽象类都是 Graham Campbell 的 Laravel Manager 包的一部分,因此您可能想查看该存储库中的文档,了解如何使用管理器类。请注意,返回的连接类始终是 AlgoliaSearch\Client
的实例。
Facades\Algolia
此 facade 将将静态方法调用动态传递到 ioc 容器中的 algolia
对象,默认情况下是 AlgoliaManager
类。
AlgoliaServiceProvider
此类不包含任何感兴趣的公共方法。应将此类添加到 config/app.php
中的提供者数组。此类将设置 ioc 绑定。
示例
在这里,您可以看到此包的使用是多么简单。默认情况下,默认适配器是 main
。在您在配置文件中输入您的认证详细信息后,它就会正常工作
// You can alias this in config/app.php. use Vinkla\Algolia\Facades\Algolia; Algolia::initIndex('contacts'); // We're done here - how easy was that, it just works! Algolia::getLogs(); // This example is simple and there are far more methods available.
Algolia 管理器将表现得像一个 AlgoliaSearch\Client
。如果您想调用特定的连接,可以使用连接方法
use Vinkla\Algolia\Facades\Algolia; // Writing this… Algolia::connection('main')->initIndex('contacts'); // …is identical to writing this Algolia::initIndex('contacts'); // and is also identical to writing this. Algolia::connection()->initIndex('contacts'); // This is because the main connection is configured to be the default. Algolia::getDefaultConnection(); // This will return main. // We can change the default connection. Algolia::setDefaultConnection('alternative'); // The default is now alternative.
如果您像我一样喜欢使用依赖注入而不是 facade,则可以注入管理器
use Vinkla\Algolia\AlgoliaManager; class Foo { protected $algolia; public function __construct(AlgoliaManager $algolia) { $this->algolia = $algolia; } public function bar() { $this->algolia->initIndex('friends'); } } App::make('Foo')->bar();
文档
此包中还有其他未在此处记录的类。这是因为该包是 官方 Algolia 搜索 API 包 的 Laravel 包装器。