久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

MVC 現在是編寫 PHP 的唯一方法嗎?

Is MVC now the only way to write PHP?(MVC 現在是編寫 PHP 的唯一方法嗎?)
本文介紹了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 的唯一方法嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 久久欧美高清二区三区 | 福利网址| 亚洲欧美精品久久 | 久久精彩视频 | www日韩欧美| 色资源在线观看 | 国产日韩欧美一区二区 | av中文字幕在线观看 | 亚欧精品 | 中文成人在线 | 最新日韩av | 看片91 | 福利视频日韩 | 波多野结衣中文视频 | 亚州精品天堂中文字幕 | 国产91综合 | 国产操操操 | 日韩精品一区二区三区中文在线 | 日韩一二区在线观看 | 操人视频在线观看 | 久久看片| 亚洲激情网站 | 国产一区二区三区日韩 | 99精品久久久 | 伊人久久一区二区 | www国产亚洲精品 | 国产一级一级毛片 | 国产 日韩 欧美 在线 | 国产亚洲精品成人av久久ww | 成人免费在线视频 | 国产视频中文字幕 | 久久久片 | 国产精品久久久久久久久久久免费看 | 亚洲91精品 | 午夜激情小视频 | 亚洲精品视频网站在线观看 | 成人妇女免费播放久久久 | 中文字幕韩在线第一页 | 日本黄色大片免费看 | 久久久精品天堂 | 奇米超碰 |