Zend Framework 2 包括自定义库 [英] Zend Framework 2 including custom library
问题描述
我的目录结构是这样的:
My directory structure is like this:
- c:\Workspaces\Zend
- c:\Workspaces\自定义库
自定义库是一个共享库,在其他应用程序中使用.它不使用命名空间,只使用旧式下划线.
Custom library is a shared library, which is in use in other applications. It doesn't use namespaces, just old style underscores.
我下载了 ZF2-Restful-Module-Skeleton,我打算将其用作 Restful 服务器.在 InfoController 我有这个代码:
I downloaded the ZF2-Restful-Module-Skeleton which i intend to use as a restful server. In the InfoController I have this code:
namespace Main\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
class InfoController extends AbstractRestfulController
{
public function getList()
{
$data = array(
'phone' => '+30123456789',
'email' => 'email@domain',
);
$Res = new CL_Res_Chain_Mutable();
return $data;
}
}
错误信息是:
致命错误:在 C:\Workspaces\Zend\module\Main\src\Main\Controller\InfoController.php 中找不到 Class 'Main\Controller\CL_Res_Chain_Mutable'
Fatal error: Class 'Main\Controller\CL_Res_Chain_Mutable' not found in C:\Workspaces\Zend\module\Main\src\Main\Controller\InfoController.php
显然,我需要将这个自定义库添加到我的 Zend 应用程序中,但我有点"迷失在这里,我真的不知道该怎么做.我在谷歌上搜索了几个解决方案,但似乎没有一个是这样的.
Obviously, I need to add this custom library to my Zend application, but Im "little" lost here, I really don't know how to do this. I have googled couple solutions, but none of them seem to be like this.
此外,我在文件夹 c:\Workspaces\Custom library 2
中有另一个库,其中包含(在其他文件中)文件(类)D.php,我像 D:: 一样使用了它:转储($数据);
Also, I have another library in folder c:\Workspaces\Custom library 2
, which has (among other files) file(class) D.php, which I have used like D::dump($data);
我怎样才能让它在我的 Zend 应用程序中像这样工作?
How can I get it to work in my Zend application like that?
推荐答案
您需要配置 StandardAutoloader
以加载您的库类.最简单的方法是修改应用程序模块的 Module::getAutoloaderConfig()
方法,使其看起来像这样:
You need to configure the StandardAutoloader
to load your library classes. The easiest way is to modify the Application module's Module::getAutoloaderConfig()
method so that it looks something like this:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
'prefixes' => array(
'CL' => 'c:\\Workspaces\\Custom library/CL',
'D' => 'c:\\Workspaces\\Custom library 2/D',
),
),
);
}
我添加了一个 prefixes
键,然后列出了前缀名称以及在磁盘上的位置.标准自动加载器文档有更多详细信息.
I've added a prefixes
key and then listed the prefix name and where to find it on disk. The Standard Autoloader documentation has more details.
如果您正在使用 Zend Skeleton Application,您也可以简单地将这些命名空间添加到您的
If you are working with a Zend Skeleton Application you may also simply add these namespaces to your init_autoloader.php file.
这篇关于Zend Framework 2 包括自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!