如何在 PHP 中实现多层架构 [英] How To Implement Multi Layered Architecture in PHP

查看:30
本文介绍了如何在 PHP 中实现多层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经确定了要在我的应用程序中实现的以下层.据我所知,多层架构更适合企业应用.

  • 展示层
  • 业务层
  • 数据访问层
  • 服务层

我选择了 Symfony2 作为应用程序中使用的框架.Symfony2 内置了 MVC 架构.上面的层存在如下.

  • 表示层=>控制器观看次数
  • 业务层、服务层 =>型号
  • 数据访问层 =>也使用了 Model 但 Doctrine.

模型由业务层、数据访问层、服务层组成.请参阅下图,该图是

如何用PHP构建n层网络架构?

在 MVC 和 3 层或 N 层架构中开发网站有什么区别?

MVC 应用程序.多层架构如何适应?

在 MVC 架构中实现服务层

使用 Symfony PHP 实现三层架构

解决方案

如果您真的想尝试制作受 MVC 启发的东西,那么您唯一的选择就是 Synfony 和 Zend.其他的只是可悲的 Rails 克隆,它们实现了 PAC 的一些混蛋.而 Sf2.x 似乎是两个中的一个更好......但这就像在辅导课上指出最聪明的孩子一样.
除非您能够并愿意制作自己的框架,否则 Sf2.x 将是您最好的选择.

我认为这里的主要问题是误解了 MVC 中的模型"是什么.模型是一个层,而不是任何特定的类或对象.在 Fowler 书中的图片中,MVC 模型"将所有三个同心圆放在一起.

您在 MVC 中基本上有两个主要层:模型层和表示层.表示层通过服务与模型层交互,让您抽象出底层的领域业务逻辑.

不要将其与领域模型"混淆.领域模型是一种描述来自客户业务的所有知识和专业知识的想法,用于创建应用程序.通常它将表示为单独的域对象和所述对象的交互.

另外,这张图有点误导.数据抽象层(通常实现为数据映射器的集合)不是域模型的低级结构.域对象和存储抽象都被服务使用(直接或通过存储库和工作单元).它被称为应用逻辑",是服务的直接责任.

底线:不,您不需要选择一些不同的框架.您只需要了解有关应用程序架构的更多信息.您应该阅读企业应用程序架构模式 ..将是一个很好的起点.

我的 2 美分

I have identified below layers to be implemented in my application. According to my knowledge multi layered architecture is prefered for an enterprise application.

  • Presentation Layer
  • Business Layer
  • Data Access Layer
  • Service Layer

I have chosen Symfony2 as the framework to be used in the app. Symfony2 has MVC architecture built into it. And the above layers exist as below.

  • Presentation Layer => Controller & Views
  • Business Layer, Service Layer => Model
  • Data Access Layer => Also Model but Doctrine is used.

Model is consisted with Business Layer, Data Access Layer, Service Layer. See the below image that is borrowed from Martin Fowler's book, Patterns of Enterprise Application Architecture.

My problem is,

is there a way to decouple the model into separate Business Layer, Data Access Layer, Service Layer ? How to implement multi Layered architecture in PHP ? Should I use some other framework(If only it supports layered architecture) ?

Update

Below link was helpful

How should a model be structured in MVC?

How to build n-layered web architecture with PHP?

What is difference of developing a website in MVC and 3-Tier or N-tier architecture?

MVC application. How does mult-tier architecture fit in?

Implementing a service layer in an MVC architecture

Achieving 3-tier architecture with Symfony PHP

解决方案

If you are serious about even trying to make something MVC-inspired, then your only options are Synfony and Zend. The others are just sad Rails-clones, that implement some bastardization of PAC. And Sf2.x seems to better one of two .. but that's like pointing out smartest kid in the remedial class.
Unless you are able and willing to make your own framework, Sf2.x will be your best bet.

I think the main problem here is the misunderstanding what "model" in MVC is. Model is a layer, not any specific class or object. In the picture from Fowler's book, the "MVC model" will be all three concentric circles taken together.

You basically have two major layer in MVC: model layer and presentation layer. The presentation layer interacts with model layer through services, that let you abstract the underlaying domain business logic.

Do not confuse it with "domain model". Domain model is an idea that describes all the knowledge and expertise from client's business, that is used in creation of application. Usually it will be represented as separate domain objects and the interaction of said objects.

Also, this picture is somewhat misleading. The data abstraction layer (usually implemented as collection of data mappers) is not lower level structure then domain model. Both domain objects and storage abstractions are uses by service (either directly or through repositories & units of work). It is referred to as "application logic" and is a direct responsibility of services.

Bottom line: no, you do not need to pick some different framework. You only need to learn more about application architecture. You should read Patterns of Enterprise Application Architecture .. that would be a good place to start.

My 2 cents

这篇关于如何在 PHP 中实现多层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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