Autoload custom library in Zend Framework 2.0

本文介绍了如何在Zend Framework 2.0中使用Autoload来加载自定义类库,并解决了在使用过程中遇到的错误。

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.

share improve this question
 
 
what? that's wrong look at the manual –  mash  Dec 19 '11 at 9:13
 
It is a Zend Framework 2. Beta1. –  plutov.by  Dec 19 '11 at 9:16
 
Why do you use an older version... (2min) –  mash  Dec 19 '11 at 9:23

5 Answers

up vote 11 down vote accepted

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',
    )
)));
share improve this answer
 
2 
Nice answer, just a little correction for this specific question: 'Garvey' => dirname(__DIR__) . '/vendor/Garvey/library/Garvey' –  Hegemon  Mar 15 '12 at 10:05
 
plutov, you shouldn't need: require_once 'vendor/ZendFramework/library/Zend/Loader/AutoloaderFactory.php';–  Matt Setter  Jan 9 '13 at 15:36

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__,
            )
        )
    );
}
share improve this answer
 
 
In my opinion this is the best answer. Using a class map is the a fast (if not the fastest) way to load classes and therefor recommended by Zend. It's also clear and easy traceable. Good job clearing this method out, thanks! –  Charlie Vieillard  Jan 16 at 20:08

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 )

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.

share improve this answer
 

I have found the answer. Put this in your index.php:

require_once 'vendor/ZendFramework/library/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader();
$loader->registerNamespace('Garvey', realpath('vendor/Garvey/library/Garvey'));
$loader->register();
share improve this answer
 
 
It is the correct way. –  Gabriel  Jan 7 '12 at 3:41 
 
mmm a little frustrating. You may be new but I'm even newer! Where does the above go? I'm thinking public/index.php –  Dominic Watson  Jan 13 '12 at 0:00
 
Yes, you correct. index.php –  plutov.by  Jan 13 '12 at 9:06
 
Actually the correct way would be to have a module for this (only adding an autoloader to the application). –  Fge  Jan 13 '12 at 10:50
2 
You are not right. Module - is a part of application. Vendor - is a part for different applications. –  plutov.by Jan 13 '12 at 10:59

Have a quick look at this post.

Now next step is add some code into our custom library.

First of all open a file ./vendor/Garvey/autoload_classmap.php

return array(

    'Garvey\Module' => __DIR__ . '/Module.php',

    'Garvey\Db\Table' => __DIR__ . '/library/Garvey/Db/Table/AbstractTable.php',

)

Next is ./vendor/Garvey/Module.php

namespace Garvey;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),

            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/library/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

Now inside your library create a file inside a folder:

./vendor/Kdecom/library/Kdecom/Db/Table/AbstractTable.php

One final thing that we need to do which is add this library into your application.config.php file.

So your application.config.php file will looks something like this way...

return array(
    'modules' => array(
        'Application',
        'Garvey'
    ),

    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),

        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
share improve this answer
 
 
Post link returns 404 –  Oliver  Oct 22 '14 at 17:11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值