How to use PHP solarium in a Laravel project

This is my second blog in a series about SOLR with the PHP Solarium library. My first blog was about the usage of OR filters to create Multi-Select facets with SOLR. With this blog item I will show you how easy it is to implement the PHP Solarium library in the Laravel framework.

SOLR Instance

This blogpost will assume that you have a running SOLR instance somewhere, if you don’t have a running SOLR instance then please follow this guide to install one on a DigitalOcean droplet — Install SOLR on Ubuntu.

In this blogpost we will integrate the code of the first blogpost from Multi-Select facets with SOLR in a new Laravel project.

Lets start with a fresh Laravel project

laravel new solarium

If you are starting fresh with Laravel and you have an OSX machine, I would suggest you start with my Starting with Laravel Valet on OSX tutorial to setup a quick local development environment. You can test your new Laravel project in the browser at the url: http://solarium.dev/.

Add a solarium configuration

The Client of solarium library uses a injection of a configuration array. Make a new file named solarium.php in the config folder and add the following content to make this configuration array.

return [ 'endpoint' => [ 'localhost' => [ 'host' => env('SOLR_HOST', '127.0.0.1'), 'port' => env('SOLR_PORT', '8983'), 'path' => env('SOLR_PATH', '/solr/'), 'core' => env('SOLR_CORE', 'collection1') ] ] ];

This configuration assumes that the SOLR is installed on the local machine, uses the default port of 8983, the default solr install path and with a default collection named collection1.
 You can override every configuration setting within the .env file in your projectfolder. If you installed SOLR with the tutorial you can add the IP of the DigitalOcean droplet by adding this to .env file.

SOLR_HOST=99.9.9.999

Where 99.9.9.999 is the IP of your droplet. If you want to know more about environment configuration please look at the guide on the Laravel site for the official environment configuration.

Install the PHP Solarium library


Originally published at www.laravelfeed.com.