PHP MVC结构 [英] PHP MVC Structure

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

问题描述

作为一个大学项目的一部分,我正在用PHP开发一个Web应用程序,并尝试尽可能地遵循MVC原则.我不使用现成的框架,因为我希望尽可能多的项目成为我的工作,以帮助获得更高的分数.

As part of a university project I'm developing a web application in PHP and trying to follow MVC principles as much as possible. I'm not using an off-the-shelf framework because I want as much of the project to be my work as possible to help get a higher mark.

要从数据库中删除一个项目(每个项目都有一个id,状态,标题,parent_id),请执行以下过程....

To delete an item (each item has an id, status, title, parent_id) from a database I go do the following process....

  • 用户点击删除按钮,然后要求确认
  • 单击确认后,将加载一个调用控制器的页面",并启动一个存储在控制器对象中的模型
  • 调用模型对象中的方法以根据URL中的ID从数据库加载项目
  • 我在模型中编辑状态属性,以将该项目标记为已删除
  • 然后我将模型保存到数据库中,实际上该模型将删除它.

我还想删除刚删除的项目的数据库中的所有子项目.最好的地方在哪里?在控制器中?在单独的功能文件中?在模型文件中?

I also want to delete all child items in the database of the item that has just been deleted. Where is the best place to do this? In the the controller? In a separate functions file? In a model file?

我对整个MVC想法还很陌生,因此非常感谢您的帮助/建议.

I'm pretty new to the whole MVC idea, so any help / advice is much appreciated.

推荐答案

MVC的基本分类是

  • 模型:业务逻辑
  • 视图:表示逻辑
  • 控制器:粘合逻辑(基于用户输入触发模型功能,基于模型输出渲染视图).

语句删除项目将导致其所有子项目也被删除"是对业务逻辑中需要发生的事情的描述.因此,它属于模型.

The statement "Deleting an item will cause all its children to also be deleted" is a description of something that needs to happen in the business logic. Therefore, it belongs in the model.

实施取决于您如何实现在模型中实现获取子项和删除项的方式,但是通常包括在其delete方法中获取模型子项的集合,并为集合中的每个子项调用delete方法(每个孩子还将寻找孩子并在发生这种情况时将其删除).现在,视图和控制器不必担心模型是否有子级,因为模型的业务逻辑中已经考虑了所有子级.

Implementation is up to how you have implemented getting child items and deleting items in your model, but it will typically consist of getting a collection of your model's children in its delete method, and invoking the delete method for each child in the collection (each child will also look for children and delete them when this happens). Now the view and controller don't have to worry about whether or not a model has children, as it's all taken care of in the model's business logic.

这篇关于PHP MVC结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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