Skip to content

Instantly share code, notes, and snippets.

@MonrealRyan
Last active April 11, 2024 04:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MonrealRyan/279d87ed532394c0f9abd559065cf868 to your computer and use it in GitHub Desktop.
Save MonrealRyan/279d87ed532394c0f9abd559065cf868 to your computer and use it in GitHub Desktop.
How to use PHP solarium to your Laravel project

SOLR installation

  1. Check (SOLR Prequisite)[https://lucene.apache.org/solr/guide/8_6/solr-system-requirements.html] Note: for Windows user you might want to use Cygwin to run solr commands.
  2. Download (SOLR)[https://lucene.apache.org/solr/downloads.html] Note: for windows user download the .zip file
  3. Unzip the file
  4. Run bin/solr start -p 8983
  5. Create collection run bin/solr create_collection -c collection1 -p 8983

PS: during this documentation SOLR version is 8.6.1

Laravel installation

  1. Create new project by running laravel new solarium
  2. Create a file name solarium.php inside config folder
<?php

return [
    'endpoint' => [
        'localhost' => [
            'host' => env('SOLR_HOST', '127.0.0.1'),
            'port' => env('SOLR_PORT', '8983'),
            'path' => env('SOLR_PATH', '/'),
            'core' => env('SOLR_CORE', 'collection1')
        ]
    ]
];
  1. Install php solarium package composer require solarium/solarium
  2. Create service provider for the solarium php artisan make:provider SolariumServiceProvider
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Solarium\Client;
use Solarium\Core\Client\Adapter\Curl;
use Symfony\Component\EventDispatcher\EventDispatcher;

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];
    }
}
  1. In config/app.php include the created SolariumProvider in the other application providers
// List off others providers...
App\Providers\SolariumServiceProvider::class,
  1. Create controller php artisan make:controller SolariumController
<?php

namespace App\Http\Controllers;

class SolariumController extends Controller
{
    protected $client;

    public function __construct(\Solarium\Client $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 (\Solarium\Exception $e) {
            return response()->json('ERROR', 500);
        }
    }
}
  1. Create ping route under routes/web.php Route::get('/ping', 'SolariumController@ping');
  2. Access ping route it should reponse OK
@shreenivasm
Copy link

Getting following error in laravel 5.8, php 7.3 & solr 5.5.5 & solarium 6.0

Solarium \ Exception \ HttpException (404)
Solr HTTP error: Neither collection nor core set. (404) #0 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/AdapterHelper.php(38): Solarium\Core\Client\Endpoint->getBaseUri() #1 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(80): Solarium\Core\Client\Adapter\AdapterHelper::buildUri(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #2 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(186): Solarium\Core\Client\Adapter\Curl->createHandle(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #3 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(39): Solarium\Core\Client\Adapter\Curl->getData(Object(Solarium\Core\Client\Request),

@MonrealRyan
Copy link
Author

MonrealRyan commented Jun 9, 2021

@shreenivasm

Getting following error in laravel 5.8, php 7.3 & solr 5.5.5 & solarium 6.0

Solarium \ Exception \ HttpException (404)
Solr HTTP error: Neither collection nor core set. (404) #0 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/AdapterHelper.php(38): Solarium\Core\Client\Endpoint->getBaseUri() #1 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(80): Solarium\Core\Client\Adapter\AdapterHelper::buildUri(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #2 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(186): Solarium\Core\Client\Adapter\Curl->createHandle(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #3 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(39): Solarium\Core\Client\Adapter\Curl->getData(Object(Solarium\Core\Client\Request),

Try this recommendation on Stackoverflow

Sorry i can't really help you with it since i set it up using Laravel 7.* and as i said in the document my version of SOLR is 8.6.1

@1810869
Copy link

1810869 commented Aug 4, 2022

Everything here is perfect! But I want to know what problem should I looking for if the ping failed?

@MonrealRyan
Copy link
Author

Everything here is perfect! But I want to know what problem should I looking for if the ping failed?

@1810869 what ping? can you provide more info?

@1810869
Copy link

1810869 commented Aug 5, 2022

Everything here is perfect! But I want to know what problem should I looking for if the ping failed?

@1810869 what ping? can you provide more info?

In SolariumController.php, we want to ping the Solr client. If it can ping the client, the 127.0.0.1:8000/ping will display 'OK". Else it will display 'ERROR'. In my case, I am getting the 'ERROR' message. I already checked my config.php, solarium.php and route.php, all seems fine. The question is, what problem should I look for to make the client ping 'OK'? I already have started the Solr Admin at the same time. I am using Solarium 6 (Solr 8.11.2) and Laravel 9 on Windows.

@MonrealRyan
Copy link
Author

@1810869 can you check if the following:

  • SOLR_HOST - should be the IP address of your SOLR server
  • SOLR_PORT - port by default is 8983 unless you change if to another port
  • SOLR_CORE - this is the name of your collection (or in MYSQL/SQL your database name)

Also, try to use the dd($e) in the catch

     // execute the ping query
        try {
            $this->client->ping($ping);
            return response()->json('OK');
        } catch (\Solarium\Exception $e) {
            dd($e);
            return response()->json('ERROR', 500);
        }

Hope this will help you in your debugging, Cheers.

@wiswebapp
Copy link

First i go through this documentation.
The only mistake in this is

  • he didnt provided adapter and $eventDispatcher
  • and other is SOLR_PATH which is /

And its working perfectly thanks 👍

P.S. my laravel is 9.X and solr version is 9.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment