简体   繁体   English

Zend Framework 2应用程序结构

[英]Zend Framework 2 application structure

Over the last few weeks, I saw a few Zend Framework 2 projects. 在过去的几周中,我看到了一些Zend Framework 2项目。 While comparing them, I noticed they basically all follow the same structure: 在比较它们时,我注意到它们基本上都遵循相同的结构:

A module named application handles all actions for the site – the "Frontend logic" for the different parts of the application (like news posts, user profile, email settings etc.). 名为application的模块负责处理站点的所有操作-应用程序不同部分(例如新闻发布,用户个人资料,电子邮件设置等)的“前端逻辑”。

For every part of the application, there is at least one module, but that only contains the "Backend logic" (adding news, deleting users etc.). 对于应用程序的每个部分,至少都有一个模块,但是仅包含“后端逻辑”(添加新闻,删除用户等)。

I think they all build on top of the Zend Framework 2 Skeleton Application. 我认为它们都基于Zend Framework 2 Skeleton Application构建。

Is this a best practice or are there any other common or recommended patterns on how to structure an application? 这是最佳实践,还是关于如何构建应用程序还有其他常见或推荐的模式吗? Why can't I write a news posts module that contains backend & frontend parts? 为什么我不能编写包含后端和前端部分的新闻发布模块?

Also, how modular should I write my modules – I mean it's possible to spread simple functions into several modules, but it may not be always wise to do so. 另外,我应该如何模块化我的模块–我的意思是可以将简单的功能分散到多个模块中,但是这样做并不总是明智的。 Are there any indicators? 有没有指标?

Is this a best practice or are there any other common or recommended patterns on how to structure an application? 这是最佳实践,还是关于如何构建应用程序还有其他常见或推荐的模式吗?

The answer is no, there is no predifined structure for Zend Framework 2 applications , contrary to ZF1. 答案是否定的,与ZF1相反,Zend Framework 2应用程序没有预定义的结构 You are free to define the structure that fits your needs. 您可以自由定义适合您需求的结构。 This is due to the Zend\\Loader\\StandardAutoloader which builds the path to the class referenced by prepending the base directory path for that namespace to the class name, and then include that Class. 这是由于Zend \\ Loader \\ StandardAutoloader通过将名称空间的基本目录路径添加到类名称之前,然后将其包含在内,从而构建了所引用类的路径 So the structure of your application folder directory can be organized as you want. 因此,您可以根据需要组织应用程序文件夹目录的结构。

This standard structure which is used by skeleton application is just a recommandation by the ZF2 Team . 骨架应用程序使用的这种标准结构只是ZF2团队的一项要求 And your are not obliged to follow it if you believe that your needs are best adapted by a different structure. 如果您认为自己的需求可以通过其他结构最好地适应,那么您就没有义务遵循它。

zf2application
├── config
│   ├── application.config.php
│   └── autoload
│       ├── global.php
│       └── local.php.dist
├── data
│       └── cache
├── init_autoloader.php
├── module
│       └── Application
├── public
│       ├── css
│       ├── images
│       ├── index.php
│       └── js
└── vendor
        ├── ZF2
        └──init_autoloader.php

The module's structure defined and recommanded by the Zend Team respects the PSR0 standard. Zend团队定义和要求的模块结构尊重PSR0标准。 You can add modules with a different structure that the one recommanded but you have to respect some rules in addition to the PSR0-standard, among other things the module.php file in the root of the module. 您可以添加结构与模块不同的模块,但是除PSR0标准外,还必须遵守一些规则,其中包括模块根目录中的module.php文件。

You could read more in the Rob Allen's post : Thoughts on module directory structure , which explains how you can change the modules directory structure whilst respecting the recommanded standards. 您可以在Rob Allen的文章: 关于模块目录结构的想法中阅读更多内容,该文章解释了如何在遵守推荐标准的同时更改模块目录结构。

Also, how modular should I write my modules? 另外,我应该如何模块化我的模块?

A module is a component which regroups one or many functionalities (frontend/backend/design) of the application. 模块是重新组合应用程序的一个或多个功能(前端/后端/设计)的组件。 The main advantage of using modules is that they separate your code and thay can be re-used in many other applications. 使用模块的主要优点是它们将代码分开,并且可以在许多其他应用程序中重复使用。 Having this idea on mind, you'll be able to know when you should create a module and when you shouldn't. 有了这个想法,您就可以知道何时应该创建模块以及何时不应该创建模块。

You could also refer to this excellent post on how to write better Zend Framework 2 modules . 您也可以参考这篇出色的文章, 了解如何编写更好的Zend Framework 2模块

This is just down to personal preference of the developer. 这仅取决于开发人员的个人喜好。 The skeleton application starts with just one module called 'Application' which is why you might see that a lot. 框架应用程序仅从一个名为“应用程序”的模块开始,这就是为什么您可能会看到很多的原因。 It is not required - I normally rename it to something more suitable. 不需要-我通常将其重命名为更合适的名称。

You can organise modules however you wish. 您可以根据需要组织模块。 You could certainly create a news module with both frontend and backend functionality - structure them in whatever way makes sense for your application. 您当然可以创建同时具有前端和后端功能的新闻模块-以对您的应用程序有意义的任何方式来构造它们。

When considering how 'modular' to make things, think about whether you might want to reuse that functionality in another project in the future. 在考虑如何以“模块化”方式进行生产时,请考虑将来是否要在另一个项目中重用该功能。 If yes, then making it into a module of its own might make sense. 如果是,那么将其放入自己的模块中可能是有意义的。

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

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