简体   繁体   English

从Zend中的库目录加载自定义类

[英]Loading custom classes from the library directory in Zend

I'm trying to load a whole set of classes from a namespace. 我正在尝试从命名空间加载一整套类。 These are just utility classes, and I don't want to treat them as action helpers, view helpers, or any sort of plugin (I have those working great thanks to the docs). 这些只是实用程序类,我不想将它们视为动作帮助程序,查看帮助程序或任何类型的插件(由于文档,我有很好的工作)。

I have the Zend-like directory structure going, eg, a class called Resources_Employee_Salary is stored under library/Resources/Employee/Salary.php. 我有类似Zend的目录结构,例如,一个名为Resources_Employee_Salary的类存储在library / Resources / Employee / Salary.php下。

Now when I'm in my action controller, I want to create an instance of Resources_Employee_Salary, but I can't because it can't find the class. 现在,当我在我的动作控制器中时,我想创建一个Resources_Employee_Salary的实例,但我不能,因为它找不到类。

What do I have to do to get Zend to load the classes under Resources/* once and for all? 我需要做些什么才能让Zend一劳永逸地加载Resources / *下的类? I looked at the Zend_Loader but that has methods which ask for a particular file or a class, I want to load the whole directory. 我查看了Zend_Loader但是有方法要求特定的文件或类,我想加载整个目录。

Any help will be appreciated. 任何帮助将不胜感激。

Thank you, 谢谢,

You need to name your classes appropriately... 你需要恰当地命名你的课程......

Path: library/Resources/Employee/Salary.php
Class: Resources_Employee_Salary

Then you need to register your namespace with the autoloader... 然后,您需要使用自动加载器注册您的命名空间...

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Resources_');

Of course you can also specify this in your projects configuration file so you dont have to do it manually... In XML format this would look like (not sure about ini format if thats what you like...): 当然你也可以在你的项目配置文件中指定这个,所以你不必手动完成...在XML格式中,这看起来像(不确定ini格式,如果那是你喜欢的...):

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  <production>
    <autoloaderNamespaces>
      <resources value="Resources_" />
    </autoloaderNamespaces>
  </production>
</application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM