Download the PHP package cviebrock/laravel-elasticsearch-handlers without Composer
On this page you can find all versions of the php package cviebrock/laravel-elasticsearch-handlers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cviebrock/laravel-elasticsearch-handlers
More information about cviebrock/laravel-elasticsearch-handlers
Files in cviebrock/laravel-elasticsearch-handlers
Package laravel-elasticsearch-handlers
Short Description Further easiness when using Elasticsearch with Laravel
License MIT
Informations about the package laravel-elasticsearch-handlers
Laravel-Elasticsearch-Handlers
An even easier way to use the official Elastic Search client in your Laravel applications.
- Installation and Configuration
- Usage
- Creating Handlers
- Special
boot
Method
- Special
- Pre-Defined Handlers
- EnvironmentIndexPrefixHandler
- Bugs, Suggestions and Contributions
- Copyright and License
Installation and Configuration
-
Install the
cviebrock/laravel-elasticsearch-handlers
package via composer: -
Publish the configuration file. For Laravel 5:
In order to make this package also work with Laravel 4, we can't do the standard configuration publishing like most Laravel 4 packages do. You will need to simply copy the configuration file into your application's configuration folder:
- Add the service provider (
config/app.php
for Laravel 5 orapp/config/app.php
for Laravel 4). The service provider needs to come after theLaravelElasticsearch
provider, since we "hijack" the Manager class from that package and use our own.
Usage
This package extends the laravel-elasticsearch
package by returning a "decorated"
Elasticsearch client class, instead of the default PHP client. You can configure
how the client is decorated on a per-connection basis.
Take the following example elasticsearch-handlers.php
configuration:
When you instantiate an Elasticsearch client with:
the package will create a base client (using the base elasticsearch.php
configuration) then wrap it in the class defined by the clientClass setting,
and inject the handlers array. So, in this case, an instance of
Cviebrock\LaravelElasticsearchHandlers\Client
is returned.
This class the bare minimum client wrapper. It doesn't do anything except pass-through all commands to the wrapped base Elasticsearch class.
Let's make it more useful ...
Creating Handlers
Pretend you only have one Elasticsearch instance running, but you need it to support indices for several of your Laravel application environments (e.g. "beta", "live", etc.)..
Instead of updating your code so that every time you index a document you make sure the right index name is specified, what if the client automatically prefixed the index name with the name of the current Laravel environment?
First, set up the package configuration like so:
Then create the Handler class (extending the BaseHandler class):
Now, every time you index a document using the default Elasticsearch connection, the current Laravel environment name will be prefixed to the index key of your data array.
This returns:
The index was prepended automatically, so your application will work across all environments without checks or changes.
Also, you can register more than one handler per connection, which means that the functionality is "chainable". E.g., prepend the environment to the index, and also add some default parameters to the body, etc..
Special boot
Method
Handlers can also define a boot
method with the following signature:
This is method is run when the handler is registered so it could be used, for example, to alter the behaviour of the client upon instantiation. The client is passed in as the only parameter.
Pre-Defined Handlers
The package ships with a few pre-defined handlers, all of which are in the
Cviebrock\LaravelElasticsearchHandlers\Handlers
namespace.
EnvironmentIndexPrefixHandler
Operates on the follow methods:
- index
This is basically the same handler as used in the example above. It will take the current Laravel environment, mangles it a bit so it matches Elasticsearch's constraints for index names, and prepends it to the index key in the given document.
Bugs, Suggestions and Contributions
Please use Github for bugs, comments, suggestions.
- Fork the project.
- Create your bugfix/feature branch and write your (well-commented) code.
- Commit your changes (and your tests) and push to your branch.
- Create a new pull request against the
master
branch.
Copyright and License
Laravel-Elasticsearch-Handlers was written by Colin Viebrock and released under the MIT License. See the LICENSE file for details.
Copyright 2015 Colin Viebrock
All versions of laravel-elasticsearch-handlers with dependencies
illuminate/support Version ~4|~5
cviebrock/laravel-elasticsearch Version ~0.9