Yii 如何将一个ZF2模块用作第三部分库?

Yii 如何将一个ZF2模块用作第三部分库?,yii,zend-framework2,autoload,Yii,Zend Framework2,Autoload,问题主要是关于ZF2,但主要任务是如何将ZF2集成到Yii 如果是ZF1,我只需要包含我需要的文件。 ZF2的结构稍微复杂一点 特别是我需要加载ServiceManager模块 我试过: $loader = new Zend\Loader\ClassMapAutoloader(); $loader->registerAutoloadMap(realpath(dirname(__FILE__) . '/lib/Zend/ServiceManager')); $loader->regis

问题主要是关于ZF2,但主要任务是如何将ZF2集成到Yii

如果是ZF1,我只需要包含我需要的文件。 ZF2的结构稍微复杂一点

特别是我需要加载ServiceManager模块

我试过:

$loader = new Zend\Loader\ClassMapAutoloader();
$loader->registerAutoloadMap(realpath(dirname(__FILE__) . '/lib/Zend/ServiceManager'));
$loader->register();
并得到一个错误:

Warning: include(/Project/lib/Zend/ServiceManager): failed to open stream: Invalid argument in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 186

Warning: include(): Failed opening '/Project/lib/Zend/ServiceManager' for inclusion (include_path='/Project/lib:.:/usr/local/zend/share/ZendFramework/library:/usr/local/zend/share/pear') in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 186

Fatal error: Uncaught exception 'Zend\Loader\Exception\InvalidArgumentException' with message 'Map file provided does not return a map. Map file: "/Project/lib/Zend/ServiceManager"' in /Project/lib/Zend/Loader/ClassMapAutoloader.php:88
Stack trace:
#0 /Project/index.php(14): Zend\Loader\ClassMapAutoloader->registerAutoloadMap('/Project/...')
#1 {main}
  thrown in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 88
Fatal error: Class 'Zend\Debug' not found in ...
根据我之前是否需要在配置数组中设置名称空间描述

有人能举例说明如何只包括一个模块吗

更新1:

我也试过这个:

require_once 'Zend/Loader/StandardAutoloader.php';

$l = new Zend\Loader\StandardAutoloader();
$l->registerPrefix('Zend', realpath(dirname(__FILE__) . '/lib/Zend/') );
$l->register();
它起作用了。之后,我打电话:

use Zend\Debug;
Zend\Debug::dump($l);
这将重新返回一个错误:

Warning: include(/Project/lib/Zend/ServiceManager): failed to open stream: Invalid argument in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 186

Warning: include(): Failed opening '/Project/lib/Zend/ServiceManager' for inclusion (include_path='/Project/lib:.:/usr/local/zend/share/ZendFramework/library:/usr/local/zend/share/pear') in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 186

Fatal error: Uncaught exception 'Zend\Loader\Exception\InvalidArgumentException' with message 'Map file provided does not return a map. Map file: "/Project/lib/Zend/ServiceManager"' in /Project/lib/Zend/Loader/ClassMapAutoloader.php:88
Stack trace:
#0 /Project/index.php(14): Zend\Loader\ClassMapAutoloader->registerAutoloadMap('/Project/...')
#1 {main}
  thrown in /Project/lib/Zend/Loader/ClassMapAutoloader.php on line 88
Fatal error: Class 'Zend\Debug' not found in ...
更新2:

此代码适用于我:

require_once 'Zend/Loader/StandardAutoloader.php';

$l = new Zend\Loader\StandardAutoloader();
$l->registerNamespace('Zend', realpath(dirname(__FILE__) . '/lib/Zend'));
$l->registerPrefix('Zend', realpath(dirname(__FILE__) . '/lib/Zend') );
$l->register();
现在我可以在项目中的任何地方包括Zend库。
无论如何,我会很高兴看到其他解决方案

我会反对你提出的解决方案,因为它有点像黑客。ZF2的一个优点是,它使您可以显式定义依赖项,以便应用程序可以轻松地与它们进行对话。因此,您可以通过composer将Yii安装为依赖项:

"yiisoft/yii": "dev-master"
如果您通过Composer安装它,您的类自动加载程序将使用对Yii框架的引用进行更新。因此,它将允许您通过FQCN访问这些文件,即:

use Yii\Path\To\Class as YiiClass;

...

$yii = new YiiClass();
或:


使用Composer还可以让您及时了解最新版本(您只需重新运行Composer脚本,它就会为您安装所有版本)。

我建议您不要使用建议的解决方案,因为这有点像黑客。ZF2的一个优点是,它使您可以显式定义依赖项,以便应用程序可以轻松地与它们进行对话。因此,您可以通过composer将Yii安装为依赖项:

"yiisoft/yii": "dev-master"
如果您通过Composer安装它,您的类自动加载程序将使用对Yii框架的引用进行更新。因此,它将允许您通过FQCN访问这些文件,即:

use Yii\Path\To\Class as YiiClass;

...

$yii = new YiiClass();
或:


使用Composer还可以让您及时了解最新版本(您只需重新运行Composer脚本,它就会为您安装所有版本)。

我有几个脚本位于ZF2正常区域之外,我需要使用框架的随机函数。我发现最简单的解决方案是:

chdir(dirname(__DIR__));

require_once 'init_autoloader.php';

\Zend\Mvc\Application::init(require_once 'config/application.config.php');
这些文件没有什么特别之处,因为它们是ZF2 Skeleton应用程序的标准配置


现在我可以使用任何ZF2函数。

我有几个脚本位于ZF2正常区域之外,我需要使用框架的随机函数。我发现最简单的解决方案是:

chdir(dirname(__DIR__));

require_once 'init_autoloader.php';

\Zend\Mvc\Application::init(require_once 'config/application.config.php');
这些文件没有什么特别之处,因为它们是ZF2 Skeleton应用程序的标准配置

现在我可以使用任何ZF2函数