简体   繁体   English

在 php mvc 项目中使用模型/控制器/视图的正确方法

[英]the right way of using models/controllers/views in a php mvc project

I have a simple mvc framework, however I am still not sure how I move on.我有一个简单的 mvc 框架,但是我仍然不确定如何继续。 Maybe its because I didnt understand the mvc pattern, but maybe its because I didn't found an more advanced example.也许是因为我不了解 mvc 模式,但也许是因为我没有找到更高级的示例。

My goal is to make a simple CMS with the MVC pattern.我的目标是使用 MVC 模式制作一个简单的 CMS。 I don't want use a framework, because the purpose of this project is to understand the MVC pattern.我不想使用框架,因为这个项目的目的是了解 MVC 模式。 I already looked at frameworks to understand it, but I found out that it doesn't make it easier, than trying to programm it myself.我已经查看了框架来理解它,但我发现它并不比尝试自己编程更容易。

The purpose (or one purpose) of OOP is to improve code reusability. OOP的目的(或一个目的)是为了提高代码的可重用性。 What I don't get is, how much do I splitt my data requests into models and where I should do the logic.我没有得到的是,我将我的数据请求分成多少模型以及我应该在哪里做逻辑。 For example: If I want to get user data, do I have a single model called user where I can get do everything that's about the user?例如:如果我想获取用户数据,我是否有一个名为 user 的 model 可以在其中获取有关用户的所有信息? So I wouldn't have a login or register model, since those methods are in the user class in the user model.所以我不会登录或注册 model,因为这些方法在用户 model 的用户 class 中。 At the end I would have a blog, with a user model and a news model... That doesn't seem to be the right way.最后我会有一个博客,用户 model 和新闻 model ......这似乎不是正确的方法。

Also not sure where the logic is, because some people say that the controller has the logic, the model has the data and the view has the styles, whereas some say that the controller shouldn't have logic in it. Also not sure where the logic is, because some people say that the controller has the logic, the model has the data and the view has the styles, whereas some say that the controller shouldn't have logic in it.

What's about dependencies?什么是依赖关系? I often read that you shouldn't have a lot of dependencies, because it makes reusablitly more difficult.我经常读到你不应该有很多依赖,因为它使重用变得更加困难。 But the "simple mvc project" has more dependencies than procedural programmed one.但是“简单的 mvc 项目”比程序编程的依赖项更多。

There are just so much opinions on the MVC pattern, that I can't move on because someone always says that it's wrong how I do it.关于 MVC 模式的意见太多了,我无法继续前进,因为有人总是说我这样做是错误的。

EDIT: Here I have uploaded what I have done so far.编辑:在这里我上传了我到目前为止所做的事情。

Hi @Mointy read this link it may help you.嗨@Mointy 阅读此链接可能会对您有所帮助。 Basics of mvc pattern mvc模式基础

I ended up doing a lot of more researches and finally got a decent knowledge about the MVC pattern.我最终做了很多更多的研究,终于对 MVC 模式有了一个不错的了解。 The videos mentioned by @dakis helped as well. @dakis 提到的视频也有帮助。 I also recommend to everyone who wants to get into MVC to search for content management systems (CMS).我还向所有想进入 MVC 的人推荐搜索内容管理系统 (CMS)。 These show a finished product with the MVC pattern where I learned a lot of from.这些展示了一个带有 MVC 模式的成品,我从中学到了很多东西。

However, the MVC pattern is only one way of doing things, there are a lot of different ways how an application can be structured.然而,MVC 模式只是一种做事方式,应用程序的结构有很多不同的方式。 MVC is a widely used one and I can recommend everyone to look into it. MVC 是一种广泛使用的技术,我可以推荐大家研究一下。

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

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