Recently I’ve been playing around with Elasticsearch and I had the need to use their PHP library with Magento. As this library wasn’t conforming to the Zend standard I had some issues getting everything up and running.

Firstly create a new folder in your lib directory. In my case I created one called lib/Elasticsearch/ then within this directory create your composer.json file, in my instance I used this.

Then navigate to the folder via command line and run the standard

This will create a vendor folder in your directory, so now you’ll have lib/Elasticsearch/vendor with the required packages and dependencies.

Now you’ll need to create a new module to get the autoloading working. Considering I’m building a Magento Elasticsearch module for Gene I called mine Gene/Elasticsearch. Once you’ve created this module create a new observer event within your config.xml for the controller_front_init_before event.

Read more about observer events here.

Within this we’re declaring that we want the controllerFrontInitBefore function to run when Magento does it’s own controller_front_init_before. This is an early event allowing us to get our autoloader in on every page that requires a controller. If you need it elsewhere then keep reading!

Now you need to create your Observer.php in your Model folder. Below is an example of the file I’m using.

You will need to alter the ‘Elasticseach’ path to the name you gave your folder within lib. I declared init as a static function so if the event wasn’t ran for any reason (say a standalone php script which just runs Mage::app()) that I’d be able to just run

Which would then declare the composer autoloader and allow me to use all the goodness within.

Instead of creating multiple sub-folders for every new package you wish to include you could have a general composer installation (say /lib/Composer) and then just update the composer.json file with the new packages you require. As I plan to package this system up this wasn’t a suitable option for me.

Dave Macaulay
Dave Macaulay
I'm an enthusiastic, slightly eccentric Magento developer who bores with the idea of another basic brief.
  • rosh3000

    Fantasticly clear and helpfull article