Zend Framework 多模块模型自动加载(models auto-loading)

众所周知,ZF是一个MVC的PHP框架,而且它能很方便的部署多模块程序。如:

ZF多模块

ZF多模块

但这样做会遇到一个问题:模块里怎么加载 models/ 下的 模型类。

现有如下解决方案:

1.把所有模块下的models写进include_path,实现自动加载。

2.把所有模型类放在library下,和Zend库同级,也能实现自动加载。

3.每次在controller里手动包含所用类的路径

除此之外,有位山人出此妙招:

<?php
abstract class System_Controller_Action extends Zend_Controller_Action
{
    //加载Model
    public function loadModel($class, $module = null)
    {
        $modelDir = $this->getFrontController()->getModuleDirectory($module)
                       . DIRECTORY_SEPARATOR . 'models';
        Zend_Loader::loadClass($class, $modelDir);

        return $class;
    }

}

回复 (0)

› 尚无评论。

发表评论

允许使用的标签 - 您可以在评论中使用如下的 HTML 标签以及属性。

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用通告 (0)

› 尚无引用通告。