Question

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

The problem I'm facing, is how best to (cleanly) setup autoloading.

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top