Zend Framework 1 and Composer

Posted by Peter
tags: php zend framework

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:

  1. 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 put vendor folder in library, 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';
    }
    
  2. 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.

  3. switching off:

    1
    2
    
    <?php
    spl_autoload_unregister(array('Zend_Loader_Autoloader','autoload'));
    
  4. now some loading happens, for example:

    1
    2
    
    <?php
    $phpExcel = new PHPExcel();
    
  5. 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 :)