1

主要问题是关于 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

根据文档,我之前需要在配置数组中设置命名空间描述吗?

有人可以举个例子请如何只包含一个模块吗?

更新 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);

这会再次出现错误:

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 库。无论如何,我很高兴看到其他解决方案

4

2 回答 2

2

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

chdir(dirname(__DIR__));

require_once 'init_autoloader.php';

\Zend\Mvc\Application::init(require_once 'config/application.config.php');

这些文件没有什么特别之处,因为它们是 ZF2 Skeleton App 的标准配置。

现在我可以使用任何 ZF2 功能。

于 2013-03-04T13:52:23.913 回答
2

我建议您反对您提出的解决方案,因为它有点像黑客。ZF2 的优点之一是它让您明确定义依赖项,以便您的应用程序可以轻松地与它们通信。因此,您可以通过 composer 将 Yii 安装为依赖项:

"yiisoft/yii": "dev-master"

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

use Yii\Path\To\Class as YiiClass;

...

$yii = new YiiClass();

或者:

$yii = new \Yii\Path\To\Class();

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

于 2013-03-02T15:22:52.430 回答