ZF 包含路径 [英] ZF Include path
问题描述
require_once 是否正确?你会在哪里以及如何放置它包含路径?
Is it correct to require_once? where and how would you put it include path?
不应该在 application.ini 或 bootstrap 中吗?
Should it not be in a application.ini or bootstrap?
示例:
require_once 'Zend/View/Helper/Abstract.php';
// @question - is this correct - where and
// how would you put it include path
class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
{
}
推荐答案
一般来说,通过适当地使用 Zend_Loader_Autoloader
,您几乎可以完全避免 require_once
调用.当然,关键是合适".
Generally speaking, you can avoid require_once
calls almost entirely by appropriately using Zend_Loader_Autoloader
. Of course, the key is "appropriate".
通常,您的 public/index.php
将 include_path
设置为 library
文件夹.然后,如果您使用 Zend_Application
,注册 Zend_Loader_Autoloader
以查找任何 PSR-0 兼容类,其命名空间前缀已使用 application/configs/application.ini
autoloadernamespaces
数组注册代码>.
Typically, your public/index.php
sets the include_path
to be the library
folder. Then, if you are using Zend_Application
, the Zend_Loader_Autoloader
is registered to find any PSR-0 compliant classes whose namespace prefixes have been registered using the autoloadernamespaces
array in application/configs/application.ini
.
棘手的部分是定义在不驻留在 include_path"的文件中的类,例如出现在 application/models
中的模型,驻留在 application/services<中的服务/code> 等.尽管那里定义的类倾向于遵循 PSR-0 标准,但 PSR-0 映射相对于包含路径的基础关闭这一事实意味着系统具有了解类名前缀和基本路径之间的映射.这就是 资源自动加载器 的用武之地.这些资源自动加载程序通常在扩展
Zend_Application_Bootstrap_Bootstrap
的应用程序 Bootstrap 和扩展 Zend_Application_Module_Bootstrap
的模块引导程序中自动设置.
The tricky part is for classes defined in files that don't "reside on the include_path", like models that appear in application/models
, services that reside in application/services
, etc. Even though the classes defined there tend to follow PSR-0 standards, the fact that the PSR-0 mapping occurs relative to a base off the include-path means that the system has to know the mapping between classname prefixes and base paths. This is where resource autoloaders come in. These resource autoloaders are typically set up automatically in the application Bootstrap extending Zend_Application_Bootstrap_Bootstrap
and module bootstraps that extend Zend_Application_Module_Bootstrap
.
视图助手是驻留在include_path"之外的类的另一个示例,可能位于类似 application/views/helpers
的地方.由于这些通常在视图脚本中使用短格式 $this->someHelper($someParam)
调用,因此必须告诉系统如何从这个短名称生成完全限定的类名.这是使用 $view->addPrefixPath()
实现的,它将命名空间前缀映射到文件系统位置.同样,应用级和模块级引导机制为您设置了其中的大部分.
View helpers are another example of classes that reside "off the include_path", perhaps in something like application/views/helpers
. Since these are typically invoked in a view script using a short form $this->someHelper($someParam)
, the system must be told how to generate the fully qualified classname from this short name. This is accomplished using $view->addPrefixPath()
which maps namespace prefixes to filesystem locations. Again, the app-level and module level bootstrapping mechanism sets most of these up for you.
对于不遵循 PSR-0 标准的库/类,您可以创建自定义自动加载器并附加它们(通常在 Bootstrap 中)到 Zend_Loader_Autoloader
单例.这是唯一可以明确包含/要求的地方.
For libraries/classes that do not follow PSR-0 standards, you can create custom autoloaders and attach them (typically at Bootstrap) to the Zend_Loader_Autoloader
singleton. This is the only place where you would have an explicit include/require.
tl;dr:通过正确使用现有的 ZF 自动加载器机制,您几乎不需要在自己的应用程序代码中使用 include/require
语句.
tl;dr: With proper use of the existing ZF autoloader mechanism, you almost never need to have include/require
statements in your own application code.
这篇关于ZF 包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!