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

查看:107
本文介绍了现在是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概念,并认为实际上大多数情况下,他们并未使用100%纯MVC时,他们正在使用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天全站免登陆