MVC 现在是编写 PHP 的唯一方法吗? [英] Is MVC now the only way to write PHP?

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

问题描述

现在可用于 PHP 的大量框架都使用 MVC.甚至 ASP.net 也有自己的 MVC 模块.

The vast amount of frameworks available for PHP now use MVC. Even ASP.net has its own MVC module.

我能看到MVC的魅力,我真的能而且我经常使用它.我能看到的唯一缺点是您必须启动整个系统才能执行页面请求.根据您的任务,这可能有点浪费.

I can see the attraction of MVC, I really can and I use it frequently. The only downside that I can see is that you have to fire up the whole system to execute a page request. Depending on your task this can be a little wasteful.

所以问题.在专业环境中,这是当今使用 PHP 的唯一方法,还是他们的其他设计方法具有替代优势?

So the question. In a professional environment is this the only way to use PHP nowadays or are their other design methods which have alternative benefits?

推荐答案

我不喜欢有人告诉我如何编写代码.如果我想使用 MVC,我会,如果我找到解决特定任务的更好方法,我会使用它.我不喜欢人们将 MVC 变成一种宗教.我还认为许多 php 编码人员误解了 MVC 概念,并认为他们使用的是 MVC 模式,而实际上大多数时候并没有使用 100% 纯 MVC.事实是,编写一个 100% MVC 并用 php 编写的网站并不容易,也不是很有效.

I don't like anyone telling me how to write a code. If I want to use MVC I will, if I find a better way that solves specific task, I will use it. I don't like when people turn MVC into a religion. I also think many php coders misunderstand the MVC concept and think they are using MVC pattern when in fact most of the times that are not using a 100% pure MVC. The truth is that it's not easy nor very efficient to write a website that is 100% MVC and is written in php.

大多数人在 MVC 的V"部分遇到最多的困难.M"很简单.这是你的数据.如果您将数据存储在数据库中并有一个数据库访问类,那就是您的M"部分,您可以使用M"

Most people having most difficulties in the "V" part of the MVC. The "M" is easy. It's your data. If you storing your data in the database and have a database access class, that's your "M" part, you good with "M"

现在控制器:当用户点击你页面的任何链接时,你的php代码必须从M"(数据库)中获取数据,准备它,应用一些逻辑,比如为登录用户添加个性化,添加请登录"链接,如果用户没有登录,等等.这是你的C"部分.

Now the controller: when user clicks on any link of your page, your php code must get the data from the "M" (database), prepare it, apply some logic like adding personalization to logged in user, adding "please login" link if user not logged it, etc. This is your "C" part.

大多数人遇到的问题是将V"视图与C"(控制器)分开这并不容易,也不是在 php 中编码的最有效方法.在许多情况下,控制器部分必须已经生成了一些 html,因此您模糊了控制器和视图之间的界限.

The problem that most people are having with is separating the "V" View from "C" (controller) This is not easy nor is it the most efficient way to code in php. In many cases the controller part must already generate some html, so you blur the line between controller and view.

如果你想保持纯 MVC,那么你必须确保你的控制器返回一个纯数据,然后 View 类需要一些模板,插入你的数据并返回 HTML.这就是 php 的问题所在.没有简单有效的方法来进行模板化,其中模板仅将纯数据作为输入并返回 html.如果有一个简单的方法,那么人们就没有理由继续想出另一个新的模板库.有这么多 php 模板库的原因是,总有程序员对任何现有的模板库不满意,并不断尝试发明自己的更好的模板库.

If you want to stay pure MVC then you must make sure that your controller returns a pure data, then the View class takes some template, plugs in your data and returns the HTML. This is where the problem lies in php. There is no easy and efficient way to do templating where a template just takes a pure data as input and returns the html. If there was an easy way, then there would not be reason for people to keep coming up with yet another new template library. The reason the there are so many templating libraries for php is because there are always programmers that are not happy with ANY of the existing ones and keep trying to invent their own, better one.

在我看来,唯一的纯模板库是 XSLT,它完全由 php 支持,它与 php 一起提供,它可以工作,它功能强大,很棒的模板引擎,更好的是,它是一种标准的、独立于平台的语言.一旦您学习了 XSLT,您就可以在任何其他编程语言(如 Java)中使用它.但是,它确实有一个小问题.它要求输入为 XML.当然,您也可以使用 DOM 库(也是 php 的一部分)在 php 中创建出色的 100% 有效 XML.问题是它会很慢.您将完成双倍的工作:首先从您的数据数组创建 XML,然后使用您的 XSL 模板将该 XML 转换为 HTML.这就是 XSLT 作为模板引擎的方法从未在 php 中兴起的原因.

The only pure templating library in my opinion is the XSLT, which is fully supported by php, it's comes with php, it works, it's powerful, great templating engine, better yet, it's a standard, platform independant language. Once you learn XSLT you can use it in any other programming language like Java. But it does have one tiny problem, however. It requires the input to be an XML. Sure, you can create a great, 100% valid XML in php also using DOM library which is also part of php. The problem is that it will be slow. You will be doing double the work: first creating XML from your array of data, then trasforming that XML into HTML with your XSL template. This is why the approach of XSLT as templating engine never took off in php.

现在你留下了其他选项来解析模板,通常是基于正则表达式的方法.当我们到达 MVC 的V"部分时,您是否看到它总是变得复杂?

Now you left with the other options to parse templates, usually its regex based approach. You see how it always gets complicated when we get to the "V" part of the MVC?

M"很简单,C"是某种类型的纯 php 代码,无论是 OOP 还是过程代码,都没有关系.棘手的是视图".

The "M" is easy, the "C" is some type of pure php code, be it OOP or procedural code, does not matter. It's the "View" that is the tricky one.

所以我的建议是 - 不要太担心坚持纯 MVC.控制器在这里和那里吐出大块的 html 是可以的.

So my advice is - don't worry too much about sticking to pure MVC. It's OK for a controller to spit out chunks of html here and there.

这篇关于MVC 现在是编写 PHP 的唯一方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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