# Integrating composer's autoloader into Magento

So composer is awesome, and there's a lot of code in composer's packagist repository that makes writing Magento extensions easier.

By default composer packages get installed to /vendor, and inside of that directory is an autoload.php file which sets up classloading for the composer managed packages.

So what would be the best way to require_once composer's '/vendor/autoload.php' file? This would need to work for both the webstore and shell scripts. Is there an observer event I can tap to do the require_once?

Cheers!

• No clue if github.com/magento-hackathon/magento-composer-installer is of any help here. I think its pretty unrelated. I think you try to install non-Magento packages with composer and want to basically have them in lib/ – Alex Mar 14 '13 at 12:47
• Yeah you can set composer to install to lib if you choose, but I want to keep composer libs/mage libs separate if possible. Also in composer you can have neat things like classmapped autoloading which doesn't fit with the Mage autoloader mechanism. – Aaron Bonner Mar 14 '13 at 13:03
• +1. Seems like one of the early event dispatches from Mage.php should do the trick...? – kalenjordan Mar 14 '13 at 13:19
• I know that this was answered a long time ago, but another option is to include the vendor autoloader in your index.php file (before require_once \$mageFilename is called). This works well for client installations, and takes no rewriting files at all. – Joseph at SwiftOtter May 5 '15 at 23:11
• Well you're hacking a core file, index.php. When you upgrade, you'll need to remember you added that line of code to index.php. – Aaron Bonner May 8 '15 at 19:42

The Magento autoloader is registered in Mage.php.

If want to do this in a own module, you should grab the first event possible and require the autoloader there

According to Alan's analysis this would be resource_get_tablename - but you have to make sure your only handle this event once.

The quick-and-dirty solution would be to patch Mage.php (the very start of the file) - but this is hacking the core.

Also we have the Magento-PSR-0-Autoloader module, which offers a way to add composers autoloading via config. Its a basic implementation which makes use of the resource_get_tablename event.

• Thanks Alex, that's perfect. I had patched mage.php but didn't want to put that in production. observing resource_get_tablename worked fine! – Aaron Bonner Mar 14 '13 at 15:02
• Vinai had a great suggestion too to put Varien_Autoload in the local code pool and put the necessary code there which avoids expensive observer events and patching core code directly. – Aaron Bonner Mar 21 '13 at 9:58
• "Simple implementation of the above example: github.com/thijssnowcountry/magento1_composer_loader" -- @user2594875 – Fabian Schmengler May 5 '17 at 7:10