Autoload custom library in Zend Framework 2.0
2015-10-29 19:02
561 查看
Autoload
custom library in Zend Framework 2.0
up vote9down votefavorite 9 | 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. php zend-framework autoload zend-framework2
| ||||||||||||
a comment |
5 Answers
activeoldestvotesup vote11down voteaccepted | 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', ) )));
| ||||||||
a comment |
up vote11down vote | Or you can defime method in Module.phppublic 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__, ) ) ); }
| ||||
a comment |
up vote3down vote | 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.phpfor 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.phpfile, rather than in the /public/index.phpfile.
| ||
add a comment |
up vote2down vote | 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();
| ||||||||||||||||||||
a comment |
up vote2down vote | 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.phpfile. So your application.config.phpfile 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', ), ), );
| ||||
a comment |
相关文章推荐
- php设计模式笔记--总结篇
- PHP报错“Parse error: syntax error, unexpected T_VARIABLE”的解决办法
- ThinkPHP函数详解:C方法
- php添加或升级扩展模块步骤
- PHP防盗链文件下载的基本思想
- 极客学院-PHP003-部署 PHP 代码[3_28]
- php优化
- sftp 限制用户登陆指定目录(家目录)
- 在php添加mongo过程中出现的mongo.so: > undefined symbol: php_json_encode in Unknown on line 0. After installation mongo driver for php 的错误
- thinkphp 隐藏index.php
- PHP XDebug Sublime Text 单步调试
- php中利用array_filter过滤数组为空值
- PHP 获取图片信息exif
- php之菜单栏用递归显示
- 基础总结篇之六:ContentProvider之读写联系人
- 基础总结篇之七:ContentProvider之读写短消息
- 基础总结篇之八:创建及调用自己的ContentProvider
- PHP利用APC模块实现大文件上传进度条的方法
- contentprovider总结(二)
- contentprovider 总结(一)