如何将Apache Solr与Laravel 9一起使用



我是Apache Solr和Laravel的新手。我想创建一个使用ApacheSolr作为搜索平台的网站,并使用Laravel作为我的PHP框架。我已经遵循了我在谷歌上找到的一些指南:

  1. https://petericebear.github.io/laravel-php-solarium-integration-20160725/
  2. https://teguharief.wordpress.com/2018/05/01/creation-search-engine-on-laravel-sites-using-solr/

我已经完成了每一步,并在SolariumServiceProvider.php 上遇到了麻烦

这是SolariumServiceProvider.php 的代码

<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use SolariumClient;
class SolariumServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Register any application services.
*
* @return  void
*/
public function register()
{
$this->app->bind(Client::class, function ($app) {
return new Client($app->['config']['solr']); <- What is the correct syntax?
});
}
public function provides()
{
return [Client::class];
}
}

solr.php已经在配置中,如图所示:

config/solr.php

然后我就犯了这个错误。

Solarium\Core\Client\Client::__construct((:参数#1($adapter(的类型必须是Solarium\Core\Client\AdapterInterface,给定数组,在中调用

任何有集成laravel和ApacheSolr经验的人都可以帮助我解决这个问题吗?

public function register()
{
$this->app->bind(Client::class, function ($app) {
$adapter = new SolariumCoreClientAdapterCurl();
$eventDispatcher = new SymfonyComponentEventDispatcherEventDispatcher();
return new Client($adapter, $eventDispatcher, $app->['config']['solr']);
});
}

使用SOLR_HOST、SOLR_PORT、SOLR_PATH、SOLR_CORE创建配置文件创建一个新的提供者并注册此代码

use IlluminateSupportServiceProvider;
use SolariumClient;
use SolariumCoreClientAdapterCurl;
use SymfonyComponentEventDispatcherEventDispatcher;
class SolariumServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Register any application services.
*
* @return  void
*/
public function register()
{
$adapter = new Curl();
$eventDispatcher = new EventDispatcher();
$this->app->bind(Client::class, function ($app) use ($adapter, $eventDispatcher) {
return new Client($adapter, $eventDispatcher, $app['config']['solarium']);
});
}
public function provides()
{
return [Client::class];
}
}

在config/app.php中AppProvidersSolariumServiceProvider::class,

最后创建一个控制器,并将此代码放入检查solr是否工作

<?php
namespace AppHttpControllers;
class SolariumController extends Controller
{
protected $client;
public function __construct(SolariumClient $client)
{
$this->client = $client;
}
public function ping()
{
// create a ping query
$ping = $this->client->createPing();
// execute the ping query
try {
$this->client->ping($ping);
return response()->json('OK');
} catch (SolariumException $e) {
return response()->json('ERROR', 500);
}
}
}

如果你仍然感到困惑,请点击这个链接

相关内容

最新更新