对模型使用Zend_Autoloader [英] Use Zend_Autoloader for Models

查看:56
本文介绍了对模型使用Zend_Autoloader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用新" Zend自动加载器加载模型吗?现在,我的配置如下所示:

does someone knows how to use the "new" Zend Autoloader to Load Models ? In the Moment my Configuration looks like this :

application.ini

application.ini

# Autoloader Namespace
autoloadernamespaces.0 = "Sl_"

Bootstrap.php

Bootstrap.php

   /**
     * Start Autoloader
     *
     * @access protected
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Sl_',
            'basePath'  => dirname(__FILE__),
        ));

        return $autoloader;
    } 

因此,当我将模型放在/Models/User.php中时,

So when I place a Model in /Models/User.php with

class Sl_Model_User{}

并创建一个新对象,一切工作都像设计的一样.但是我如何使用自动加载器加载放置在/Models/Dao/UserDB.php中的模型?

and create an new object , everything works like designed. But how can i use the Autoloader to load a Model placed in /Models/Dao/UserDB.php ?

class Dao_UserDB{}

推荐答案

检查Resource_Autoloader上的文档(其目的是加载驻留在models目录或其他位置(即/library文件夹之外)的资源.

Check the documentation on the Resource_Autoloader (its purpose is to load resources that reside in the models directory or elsewhere - i.e outside the /library folder).

资源自动加载器旨在管理遵循Zend Framework编码标准准则的命名空间库代码,但是在类名和目录结构之间没有1:1映射.它们的主要目的是促进自动加载应用程序资源代码,例如特定于应用程序的模型,表单和ACL.

"Resource autoloaders are intended to manage namespaced library code that follow Zend Framework coding standard guidelines, but which do not have a 1:1 mapping between the class name and the directory structure. Their primary purpose is to facilitate autoloading application resource code, such as application-specific models, forms, and ACLs.

资源自动加载器在实例化时向自动加载器进行注册,并为其关联的名称空间.这样一来,您就可以轻松地在特定目录中对名称空间的代码进行命名,同时还能获得自动加载的好处."

Resource autoloaders register with the autoloader on instantiation, with the namespace to which they are associated. This allows you to easily namespace code in specific directories, and still reap the benefits of autoloading."

path/to/some/directory/
    acls/
         Site.php
    forms/
        Login.php
    models/
        User.php


$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath'  => 'path/to/some/directory',
'namespace' => 'My',

));

$resourceLoader->addResourceTypes(array(
'acl' => array(
    'path'      => 'acls/',
    'namespace' => 'Acl',
),
'form' => array(
    'path'      => 'forms/',
    'namespace' => 'Form',
),
'model' => array(
    'path'      => 'models/',
),

));

在您的boostrap文件中尝试一下:

Try this in your boostrap file:

protected function _initLoaderResource()
{
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => 'your_doc_root' . '/application',
        'namespace' => 'MyNamespace'
    ));
    $resourceLoader->addResourceTypes(array(
        'model' => array(
            'namespace' => 'Model',
            'path'      => 'models'
        )
    ));
}

这篇关于对模型使用Zend_Autoloader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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