Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoload custom library in Zend Framework 2.0

I need to use autoloading for my custom classes in Zend Framework 2.0. My custom library located in /vendor/Garvey/library/Garvey. I have a simple extended AbstractTable class in /vendor/Garvey/library/Garvey/Db/Table/AbstractTable.php:

<?php

namespace Garvey\Db\Table;

use Zend\Db\Table\AbstractTable;

abstract class AbstractTable extends AbstractTable
{
    public function getItemById($id)
    {

    }
}

In the index.php I have the following code:

require_once 'vendor/ZendFramework/library/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
    'prefixes' => array(
        'Garvey' => 'vendor/Garvey/library/Garvey',
    )
)));

But I have the following error. What I have missed?

Fatal error: Class 'Garvey\Db\Table\AbstractTable' not found

Thank you in advance.

like image 480
Alex Pliutau Avatar asked Dec 19 '11 09:12

Alex Pliutau


3 Answers

Your original index.php would also worked if you changed the 'prefixes' key to 'namespaces' and specify path like below:

Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
    'namespaces' => array(
        'Garvey' => dirname(__DIR__) . '/vendor/Garvey',
    )
)));
like image 76
atti Avatar answered Oct 21 '22 05:10

atti


Or you can defime method in Module.php

public function getAutoloaderConfig()
{
    $return = array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php'
        ), 
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                'Garvey' => __DIR__ . '/../../vendor/Garvey/library/Garvey',
            )
        )
    );
}

But I would not recommend it. Since ZF2 purpose all centered about speed in autoloading the best way is to use class_map style to load your classes. It will work much quicker at the end but require additional work. You can to register every class in you class_map file.

You can create class_map.php in the root of your library and place there

<?php
return array(
    'Garvey\Db\Table\AbstractTable' => __DIR__ . '/Garvey/Db/Table/AbstractTable.php', 
);

And add there as many classes as you use. And in getAutoloaderConfig() you can add you classmap

public function getAutoloaderConfig()
{
    $return = array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
            __DIR__ . '/../../vendor/Garvey/library/Garvey/class_map.php',
        ), 
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            )
        )
    );
}
like image 45
Sergey Romanov Avatar answered Oct 21 '22 06:10

Sergey Romanov


Matthew Weier O'Phinney explains in this video that there are now 3 methods for autoloading :

  • ZF1-style include_path autoloader ( old zf1 method, not recommended )
  • Per-namespace/prefix autoloading ( new zf2 method, better )
  • Class-map autoloading ( recommended and the fastest )

A class-map generator utility is mentioned in the docs that will take care of writing the /vendor/vendor_name/library/autoload_classmap.php for you.

The solution you found is similar to the one Matthew mentions in the video for the Per-namespace/prefix autoloading. Following the code structure in ZendSkeletonApplication, that code would go in the /init_autoloader.php file, rather than in the /public/index.php file.

like image 3
dan_nl Avatar answered Oct 21 '22 04:10

dan_nl