使用 StandardAutoloader 自动加载 Zend Framework 2 包 [英] Autoloading Zend Framework 2 packages with the StandardAutoloader

查看:28
本文介绍了使用 StandardAutoloader 自动加载 Zend Framework 2 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前使用的是 Zend Framework 2 的下载版本,并且基本上能够执行以下操作:

I was previously using a downloaded version of Zend Framework 2 and was basically able to do something like this:

// Set include paths (add Zend to the path)
set_include_path(get_include_path() . PATH_SEPARATOR . $__CONFIG['zendPath']);

// Setup the Zend Autoloader
require_once('library\Zend\Loader\StandardAutoloader.php');

$autoLoader = new StandardAutoloader(array(
    'namespaces' => array(
        'Zend' => $__CONFIG['zendPath'] . '/library/Zend'
    )
));

这是有效的,因为所有 Zend 包都在库文件夹中.我现在想使用 composer 只加载我需要的 Zend 包.我遇到的问题是包按以下方式排列:

This works because all Zend packages are inside the library folder. I now wanted to use composer to load only the Zend packages I need. The problem I'm running into is that the packages get arranged in the following way:

zendframework/zend-cache/Zend/...
zendframework/zend-loader/Zend/...
zendframework/zend-validator/Zend/...
etc.

我尝试在 StandardAutoloader 中使用单独的命名空间声明,如下所示:

I tried having separate namespace declarations in the StandardAutoloader like this:

$autoLoader = new StandardAutoloader(array(
    'namespaces' => array(
        'Zend\Cache' => $__CONFIG['zendPath'] . '/library/zend-cache/Zend/Cache',
        'Zend\Loader' => $__CONFIG['zendPath'] . '/library/zend-loader/Zend/Loader',
        'Zend\Validator' => $__CONFIG['zendPath'] . '/library/zend-validator/Zend/Validator'
    )
));

那行不通.假设命名空间中可能没有反斜杠?有什么办法可以使这项工作?最好不必定义每个单独的包.

That doesn't work. Am assuming the namespace probably can't have the backslashes in it? is there any way to make this work? Preferably without having to define each individual package.

推荐答案

你只需要修复路径:

$libs = $__CONFIG['zendPath'];
$autoLoader = new StandardAutoloader(array(
    'namespaces' => array(
        'Zend\Cache' => $libs . '/library/zend-cache/Zend',
        'Zend\Loader' => $libs . '/library/zend-loader/Zend',
        'Zend\Validator' => $libs . '/library/zend-validator/Zend',
    )
));

这应该可以解决问题(它未经测试,所以我不确定尾随的 Zend

That should do the trick (it's un-tested, so I'm not sure about the trailing Zend

这篇关于使用 StandardAutoloader 自动加载 Zend Framework 2 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆