Zend Framework 1 and Composer
Recently I found out, that Zend Framework 2 fully supports Composer - gem like package manager for PHP. But I couldn’t find a way to use it with Zend Framework 1 because of it’s defaultĀ ZendLoaderAutoloader behavior. I really wanted to use it so I came up with dirty hack, here it is:
First you have to add
require 'vendor/autoload.php';
in order to load Composer autoloader. I do it in Bootstrap.php file: (By the way as you can see I'v putvendor
folder inlibrary
, just to keep things organized.)1 2 3 4 5 6 7 8
<?php /** * loading Composer autoloader. */ protected function _initComposer() { require '../library/vendor/autoload.php'; }
now we have (theoretically) access to libraries provided by Composer, but we can’t load them, because ZendLoaderAutoloader is in our way, trying to do it’s thing and load classes from appropriate folder structure. We have to bypass it, by turning it off, accessing what we need from Composer, and turning it on again, so it will be available for further auto loading.
switching off:
1 2
<?php spl_autoload_unregister(array('Zend_Loader_Autoloader','autoload'));
now some loading happens, for example:
1 2
<?php $phpExcel = new PHPExcel();
and now in order to bring it back:
1 2
<?php pl_autoload_register(array('Zend_Loader_Autoloader', 'autoload'));
I know it’s dirty, but works for me, and actually performs really well :)