Zend 自动加载模型问题
Zend 框架。 我想从 bootstrap 类中自动加载 models 文件夹中的模型类。 这些模型实际上并不使用任何命名空间(所以我有 Ex. User.php 文件的类名为 User 等等......)。
如果我理解正确,我应该使用 Zend_Loader_Autoloader_Resource 并且我尝试过:
function _initLoaderResource()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,//points to the "application" path where resides "models" folder
'namespace' =>''
));
$resourceLoader->addResourceType('models', 'models/');
}
我收到以下“Zend_Loader_Exception”消息:
'Initial definition of a resource type must include a namespace'
我的问题是:
- 这是自动加载模型的正确方法吗?
- < strong>如何管理不遵循 Zend Framework 编码标准的资源代码?
Zend framework.
I want to autoload my models classes inside models folder, from within bootstrap class.
These models doesnt actually use any namespace (so I have Ex. User.php file's class named User and so on..).
If I understood correctly I should use the Zend_Loader_Autoloader_Resource and I tried:
function _initLoaderResource()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,//points to the "application" path where resides "models" folder
'namespace' =>''
));
$resourceLoader->addResourceType('models', 'models/');
}
And I receive following 'Zend_Loader_Exception' message:
'Initial definition of a resource type must include a namespace'
My questions are:
- Is this the right way to autoload models?
- How should I manage resource code that doesn't follow Zend Framework coding standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您可能不想为此使用资源自动加载器,因为(正如您所发现的)它需要一个命名空间。标准自动加载器(从包含路径加载模型)有一个选项
setFallbackAutoloader
,它告诉 ZF 该自动加载器应该用于与另一个类覆盖的命名空间不匹配的任何类。因此,您需要做的就是确保您的模型目录位于包含路径中并将此选项设置为 true。您可能已经在使用标准自动加载器来加载 Zend 类,因此您可能需要修改 application.ini 文件以将模型目录添加到包含路径中,然后在 application.ini 或中设置回退选项你的引导类:
Actually you probably don't want to use the resource autoloader for this, since (as you've discovered) it requires a namespace. The standard autoloader (which loads models from the include path) has an option
setFallbackAutoloader
which tells ZF that that autoloader should be used for any class not matching a namespace covered by another. So all you need to do is ensure your models directory is on the include path and set this option to true.You are probably already using the standard autoloader for loading the Zend classes, so you'll probably want to modify your application.ini file to add your model directory to the include path, and then set the fallback option either in application.ini or in your Bootstrap class:
Zend Autoloader 使用命名空间来确保您没有在那些您不需要的类上使用自动加载过程。所以你必须为你的类选择一个命名空间。
您可以使用特定于应用程序的命名空间或通用命名空间来启动您的类。
“My_”或“App_”等命名空间是通用的,但例如,如果您的应用程序名称是 Job Board,则可以在类文件中使用“JB_”等命名空间。
您还可以编写自己的自动加载器(可以是全新的自动加载器,也可以通过扩展 Zend 自动加载器)并将其注册为 SPL 自动加载器 来绕过这个。
您的类名不必遵循 Zend Framework 命名约定,只需确保它们具有命名空间并在自动加载器中注册该命名空间即可。
这里我附上一段代码,用于注册一些要自动加载的资源。我有多个模块,每个模块都有一个与该模块名称相关的命名空间。请注意,由于有很多命名空间,我将它们全部注册在一个循环中。
当您通过调用定义资源类型时:
您实际上是在告诉 Zend Autoloader 您有一个类型“service”(第一个参数),该类型放置在名为“services”(第二个参数)的目录中,并且您正在使用“<类名称中的 em>Service' 标记用于指定此类型的类。
上面的代码告诉 Zend Autoloader 在路径“APPLICATION_PATH/modules/store/services/Core.php”中搜索类 Store_Service_Core。
正如您所看到的,我已经为 APPLICATION_PATH 路径注册了通用“应用程序”命名空间。这意味着以 Application_ 开头的每个类都将从 APPLICATION_PATH 中自动加载。例如,我有一个名为 Application_Init 的类,它使用一些初始化任务,现在 Zend 从路径 APPLICATION_PATH/Init.php 自动加载它。
Zend Autoloader uses namespaces to make sure you are not using the autoload process, on those classes you don't want. So you would have to choose a namespace for your classes.
You could start your classes with an application specific namespace, or a general one.
namespaces like 'My_' or 'App_' are general, yet for example if your application name is Job Board, you could use namespaces like 'JB_' in your class files.
You may also write your own autoloader (either a totally new one, or by extending the Zend autoloader) and register it as the SPL autoloader to bypass this.
Your class names does not have to follow the Zend Framework naming conventions, just make sure they have a namespace and register the namespace in the autoloader.
Here I attach a piece of my code that registers some resources to be autoloaded. I'm having multiple modules, and each module has a namespace regarding that module name. Please note that since there were many namespaces, I register them all in a loop.
When you are defining a resource type by calling:
You are actually telling Zend Autoloader that you have a type 'service' (1st param), which is placed in the directory named 'services' (2nd param), and you are using 'Service' token in the class names to specify classes of this type.
The above code tells Zend Autoloader to search for class Store_Service_Core in the path 'APPLICATION_PATH/modules/store/services/Core.php'.
As you can see I have registered the general 'Application' namespace for the APPLICATION_PATH path. This means that each class, starting with Application_ would be autoloaded from the APPLICATION_PATH. So forexample I have a class named Application_Init which uses some initialization tasks, and now Zend autoloads it from the path APPLICATION_PATH/Init.php.