模型不是类或任何单个对象。犯[...]是一个非常常见的错误,因为大多数框架都会延续这种误解。
那么,模型的最佳文件夹结构是什么?
例如,Zend推荐的项目目录结构只有一个"模型"文件夹。但是,当我尝试将模型分为域对象、数据映射器和服务时,此结构应该是什么样子的?
谢谢!
我认为
这是主观的,但我会给你我的方式。我保留models
目录,然后为应用程序的每个模块创建不同的子目录。它看起来像下面这样:
application
- controllers
- models
- authentication
- services
- mappers
- ...
- mail
- services
- mappers
- ...
... (other directories)
- views
- templates
我觉得这很好地分离了每个模块,同时将所有内容保留在其他开发人员正确习惯的models
目录中。
我不知道这是最好还是最有效的解决方案,但我认为通过正确使用命名空间,它证明很容易管理。我还了解到,如果您正确使用SOLID principle
,您可以(几乎)将不同的目录/模块复制/粘贴到其他项目中,而无需太匆忙。
TutsPlus创建的小系列,通过使用理论和具体示例详细解释了SOLID原理。
我希望这能帮助你找到正确的方向。此致敬意。
在Zend Framework中,你可以创建可以集成自己的模型的"模块"。
application/
configs/
application.ini
controllers/
helpers/
forms/
layouts/
filters/
helpers/
scripts/
models/
modules/
Domain Objects/
controllers/
models/
Data Mappers/
controllers/
models/
Services/
controllers/
models/
services/
views/
filters/
helpers/
scripts/
Bootstrap.php
如您所见,每个模块还具有关联的控制器和视图。
最后,您必须将这些模型添加到引导程序的自动加载器中:
// Admin
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH . '/modules/Domain Objects',
'namespace' => 'Domain Objects'
));
$resourceLoader->addResourceType('controller', 'controllers/', 'Controller')->addResourceType('model', 'models/', 'Model');