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

如何在我的 PHP/SQL/HTML/CSS 代碼上實現 MVC 風格?

How to implement MVC style on my PHP/SQL/HTML/CSS code?(如何在我的 PHP/SQL/HTML/CSS 代碼上實現 MVC 風格?)
本文介紹了如何在我的 PHP/SQL/HTML/CSS 代碼上實現 MVC 風格?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在開發用于可視化某些數據的程序.我的程序從 MySQL 數據庫獲取特定輸入并繪制一些圖形(libchart 庫),創建一些表格等.

I have been developing a program for the visualization of some data. My program takes specific input from a MySQL database and draws some graphs (libchart library), creates some tables etc.

我的問題是現在它是一個代碼地獄.我有大約 7 個帶有 HTML/CSS 和 PHP/SQL 代碼的 php 文件(索引、圖形頁面、圖庫等)(其中一些只有 php 擴展名,但里面只有 HTML).我暫時閱讀和理解該項目沒有問題,但我想如果其他人嘗試,他可能會頭疼.另外,繼續這樣的編程是不切實際的,因為該項目將來可能不容易擴展.

My problem is that right now its a code hell in there. I have around 7 php files (index, graph-page, gallery, etc) with HTML/CSS and PHP/SQL code all together (some of them just have the php extension but have only HTML inside). I have no problem to read and understand the project for the time being, but I guess if someone else tried to, he might get a headache. Plus, continuing programming like this is not practical because the project might not be easily scalable in the future.

您對如何成功地將 HTML/CSS 與 PHP/SQL 分開有什么建議嗎?我不想使用框架,因為我沒有做任何需要用戶輸入、會話處理等的事情.我只是運行一些查詢并將結果可視化.我在這里主要談論架構,如果適用,也許可以幫助我的腳本(我已經閱讀了 Smarty,但我不確定這是否是我需要的).

Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL? I don't want to use a framework since I'm not doing anything that requires user-input, session handling, etc. I just run some queries and visualize the results. I'm mostly talking about architecture here, and if applicable perhaps a script to help me (I've read about Smarty but I'm not sure if that's what I need).

推薦答案

對于如何成功地將 HTML/CSS 與 PHP/SQL 分離,您有什么建議嗎?

Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL?

恭喜您了解如何改進代碼.這是前提條件,你需要想改進它,而且這個話題很長.所以你的意愿很重要.

Congrats for looking how you can improve code. That's the precondition, you need to want to improve it and the topic is lengthy. So your will is crucial.

我很輕松地開始,然后嘗試提供一些提示.如果您缺少經驗,請尋找一個起點,當然是下面列表中的最后一個.但首先要注意的是:

I start lightly and then try to give some tips. As you're missing experience, look for one point to start with, most certainly the last one of the list below. But first things first:

要將某些東西彼此分開,您需要有一些代碼來分開:

To separate something from each other, you need to have some code that separates:

[HTML/CSS/PHP/SQL]

[HTML/CSS] <--> [SEPARATOR] <--> [PHP/SQL]

這里的分隔符實際上也是 PHP 代碼,但我想你明白了.

The Separator here actually is PHP code as well, but I think you get the idea.

正如您所看到的,只有 SeparatorHTML/CSSPHP/SQL 對話.

As you can see only the Separator talks with HTML/CSS and PHP/SQL.

所以 HTML/CSSPHP/SQL 都需要有一個帶有 Separator(之間的線)的接口來完成這項工作.

So both HTML/CSS and PHP/SQL need to have an interface with Separator (the line between) to make this work.

通常在程序中,您會傳遞經過處理的數據.數據非常動態,可能具有復合復雜性,尤其是當您將數據傳遞給應正確格式化的輸出例程時.

Normally in a program you pass around data that get's processed. The data is pretty dynamic and can have a compound complexity, especially if you pass data to an output routine that should format it properly.

有多種方法可以編寫這樣的分隔符(或多個分隔符).您可以分層您的軟件或提供在其區域中執行操作的組件.例如.你有一個數據庫層數據庫組件來處理與數據庫的交互.

There are multiple ways of how such a Separator (or multiple of them) can be written. You can layer your software or provide components that do things in their area or domain. E.g. you have a database layer or database component that takes care about the interaction with the database.

或者您有一個模板引擎,負責將您的字符串和數組放入一些可讀的 HTML 中.

Or you have a templating engine that takes care to put your strings and arrays into some readable HTML.

簡而言之,這就是軟件設計的通心粉理論:

In short this is the pasta theory of software design:

  • 意大利面代碼 - 合而為一,代碼高度交織,最好與 Bolognese 或 Aglio、Olio e Peperoncino 搭配.
  • 千層面代碼 - 分層,一層有另外兩層與之交互(除非底部或頂部),總是用白醬汁.
  • Tortelini 代碼 - 小部件,只做自己的工作,里面有肉或辣蔬菜.
  • Spaghetti code - all in one, code is heavily interwoven, preferable with Bolognese or Aglio, Olio e Peperoncino.
  • Lasagne code - Layered, one layer has two other layers it interacts with (unless bottom or top), always with Béchamel sauce.
  • Tortelini code - Small components that just do their job, they have Meat or Spicy Vegetables inside.

就像我們在生活中吃不同的意大利面一樣,在編程時我們也需要處理所有這些不同類型的代碼,并且隨著時間的推移我們會形成自己喜歡的口味.小時候我們吃飽,但隨著時間的推移,我們開始自己做飯并改變食譜.

Like we eat different pasta in our lives, when programming we need to deal with all these different type of code as well, and we develop our own preferred taste over time. As a kid we're feed but over time we start to cook something our own and vary the recipes.

所以我認為這是一個很好的觀點,你只是不想在接下來的幾周內吃 MVC Framework X 和很多很棒的,只是因為有人告訴你這是現在吃的方式.在吃之前,有品嘗,對吧?更不用說快餐了,你知道像這些帶醬汁的面條——只加水.呃.

So I think it's a good point you just don't want to now eat MVC Framework X with much awesome for the next weeks only because somebody told you it's the way to eat now. And before eating, there is tasting, right? Not to mention fast-food, you know like these noodles with sauce in package - only add water. Urgh.

我不知道您的輸出需要哪些數據以及輸入是什么.以下是輸出 HTML/CSS 并與 MySQL 數據庫交互的應用程序的一些粗略重構技巧.這不能是一個完整的列表,描述只能粗略地勾勒出一些想法:

I don't know which data your output needs and what the input is. Following are some rough refactoring tips for applications that output HTML/CSS and interact with a MySQL database. This can not be a complete list and the descriptions can only roughly outline some thoughts:

  • 將 CSS 移出 HTML.在鏈接的 CSS 定義中有效地使用選擇器并替換任何 style 屬性(如果您還有一些).這使您的 CSS 代碼可重用且更加模塊化.它將幫助您找到 HTML 中的缺陷并將結構 (HTML) 與演示文稿 (CSS) 分開.有效的 HTML 始于對 CSS 的有效使用,這兩者結合起來非常強大,通常這已經可以減輕您的程序輸出例程.
  • 將業務邏輯從 HTML 中移出.HTML 和您的代碼都可能是野獸,因此最好將它們分開.它們有相互爭斗的傾向,而且由于兩者都非常強大,因此在您開發應用程序時爭斗將繼續,這會分散您對需要做的工作的注意力.
    考慮一下您是否需要在應用程序中已經有復雜的輸出,或者您是否可以只傳遞帶有子元素的數組(一個鍵是一個 var,一個 var 可以包含一個字符串或數字或另一個 var 數組).通常,即使是將復雜的數據傳遞到視圖模板,都需要這樣做.然后,您的 HTML 只需要在子數組上回顯一些數組成員和/或 foreach.這是創建模板的一種非常簡單的技術.您可以使用 PHP,因此您實際上非常靈活(只需繪制屬于您的視圖層和應用程序的一部分的代碼的邊框,例如為視圖提供值).
  • 將 SQL 從代碼中移出.將數據庫交互代碼移開.自己創建一個或多個對象,這些對象具有在實際處理代碼中以您需要(使用)數據的方式返回數據的方法,例如 $component->getThatData() 然后以一種方式返回數據規范化形式.制作這些組件,然后使用專用的數據庫組件與數據庫進行對話.在您的應用程序代碼(業務邏輯)中,僅使用數據庫組件,最好使用您創建的對象來獲取數據,因此您的主代碼中不再有任何 SQL 行.
  • 分而治之您的應用程序代碼:將您的代碼分成事務腳本.它們通常很容易從現有的意大利面條式代碼創建,并且很可能成為您在中間尋找的所述分隔符.然后,他們將負責處理數據并將其傳遞(到輸出/視圖中).
  • 使用清晰的語言:如果您有未規范化的復雜格式化字符串數據,請自己編寫Parser 類來為您完成工作并且可以輕松重新使用(如果您的應用程序是這種情況).由于您應該期待盡量減少在代碼中使用純 SQL,您還應該期待將復雜的正則表達式移走.封裝變化是一個關鍵點.這同樣適用于僅處理某些數據的長例程(例如,以另一種格式對其進行排序、排序和排列),將它們移動到各自的組件中,并考慮如何使它們可訪問和可重復使用.
  • 使您的代碼正常運行:了解您如何在程序中調用功能的邏輯.您可以嘗試將功能與其調用方式分開.例如.一些調用任何事務腳本的例程.如果您直接通過瀏覽器請求 PHP 文件,這可能不是必需的,因為這些是您的交易腳本,并且網絡服務器會小心地將通過 URL 發送到您的應用程序的命令解析為交易腳本.但是,您應該將處理傳入命令及其參數所需的任何邏輯包裝到可重用的組件中(例如,Request 類包含用于獲取 URL 和或來自 HTTP 請求的變量).
  • 通過在通過瀏覽器調用的所有文件的最頂部包含相同的文件來創建一個公共入口點.然后,您可以將通用代碼(例如設置應用程序會話狀態 對象和初始化數據庫組件)放入其中,另請參見 應用控制器
  • 通過查找字面上重復的代碼來消除重復.將它包裝成一個函數或類.為您自己的應用程序創建一個庫文件夾,將包含的內容放入其中.如果您遵循類名和命名空間的通用模式,您可以輕松使用自動加載器來保持輕松包含.使您的庫與第三方代碼分開.將所有第三方代碼放入自己的庫文件夾中,每個第三方組件都有一個子目錄.
  • 使用輕量級的現有組件.輕量級很重要,因為您已經擁有自己的代碼,您不想一次性將其全部轉換并壓在框架上.現有很重要,因為您不想重新發明輪子.您將有足夠的工作來重構您的代碼.在您對您的應用程序感覺更好并且您仍然擁有權力和意愿之后,您可以隨時編寫新的內容.但是,如果您是獨自一人或在一個小團隊中,Existing 非常強大.簡單的庫例如:
    • 模板引擎:Mustache
    • 數據庫層:NotORM
    • Move CSS out of HTML. Use selectors effectively in the linked CSS definition and replace any style attributes if you still have some. This makes your CSS code re-useable and more modular. It will help you to find flaws inside your HTML and to separate the Structure (HTML) from the Presentation (CSS). Effective HTML start with effective usage of CSS, those two are very powerful together and often this already will lighten your programs output routines.
    • Move business logic out of HTML. Both HTML and your code can be a beast, so better keep them apart. They have the tendency to fight with each other, and as both are very powerful, the fight will continue while you develop your application, that distracts you from the work you need to do.
      Consider if you need to already have complex output inside your application or if you can just pass on arrays with subelements (a key is a var, a var can contain a string or number or another var-array). Normally that is all needed to pass even complex data into a view or template. You HTML then only needs to echo some array members and or foreach over subarrays. This is a very simple technique to create a template. You can use PHP for it, so you're actually really flexible (just draw the border which code belongs into your view layer and which is part of the application, e.g. providing values for the view).
    • Move SQL out of your code. Move the database interaction code away. Create yourself one or multiple objects that have methods which return the data in a way you need (consume) it in your actual processing code, like $component->getThatData() which then returns data in a normalized form. Make those components then use a dedicated database component to talk over with the database. In your application code (business logic) only use the database component and preferably the objects you create to get the data, so you don't have any line of SQL any longer inside your main code.
    • Divide and Conquer your application code: Divide your code into Transaction Scripts. They are often easy to create from existing spaghetti code and will be probably become the said Separator you're looking for in middle terms. They will then have the role to process data and passing it on (into the output/view).
    • Use clear language: If you have complex formatted string data that is not normalized, write yourself Parser classes that do the work for you and which can be easily re-used (if that's the case in your application). As you should look forward to minimize the use of plain SQL in your code, you should also look forward to move complex regular expressions away as well. Encapsulate what varies is a key point. Same applies to long routines to just handle some data (e.g. sort, order and arange it in another format), move them into components of each own and think about how you can make them accessible and re-useable.
    • Make your code functioning: Find out about the logic how you invoke functionality in your program. You can try to separate functionality away from how it's invoked. E.g. some routine that invokes any of the Transaction Scripts. This might not be necessary if you request PHP files directly via the browser, as those are then your transaction scripts and the webserver takes care to resolve the command send via URL into your application to the transaction script. But you should then wrap any logic needed to process the incoming command and it's parameters into re-useable components (e.g. a Request class that contains standard code to get the URL and or variables from a HTTP request).
    • Create a common entry-point by including the same file at the very top of all files that are called via the browser. You can then put common code (like setting up the application session state object and initializing the database component) into it, see as well Application Controller
    • Remove duplication by looking for literally duplicated code. Wrap it into a function or class. Create a library folder for your own application into which you put your includes. If you follow a common pattern with Classnames and Namespacing, you can easily use an autoloader to keep inclusion easy. Make your library apart from third-party code. Place all third-party code into a library folder of it's own with one subdirectory for each third-party component.
    • Use lightweight, existing components. Lightweight is important because you have your own code already, you don't want to turn and press it all at once onto a framework. Existing is important because you don't want to re-invent the wheel. You will have enough work for your own refactoring your code. After you feel better about your application and you still have power and will, you can always write everything new. But if you're alone or in a small team, Existing is pretty powerful. Simple libraries are for example:
      • Templating engine: Mustache
      • Database layer: NotORM

      這篇關于如何在我的 PHP/SQL/HTML/CSS 代碼上實現 MVC 風格?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 示例)
主站蜘蛛池模板: 久久久免费在线观看 | 亚洲a一区| 日本黄色的视频 | 偷牌自拍| 亚洲最大福利网 | 91麻豆产精品久久久久久夏晴子 | 青青草网站在线观看 | 日本三级播放 | 精品国产一区二区三区日日嗨 | 激情久久久久 | 久久久亚洲成人 | 久草.com| 亚洲国产成人精品久久久国产成人一区 | 美女毛片免费看 | 久久精品男人的天堂 | 欧美日韩亚洲二区 | 色黄网站 | 国产成人网| 久久精品成人 | 久草在线青青草 | av一区二区三区 | 伊人欧美视频 | 欧美精品成人 | 成年人黄色免费视频 | 亚洲免费在线观看 | 亚洲视频一区在线 | 国产一区二区麻豆 | 国产一区二区在线免费观看 | 日韩成人在线视频 | 欧美日韩高清免费 | 精品视频在线观看 | 一区二区三区在线免费看 | 免费在线观看av | 日韩精品色网 | 五月婷六月丁香 | 亚洲精品电影在线观看 | 自拍偷拍欧美 | 一级欧美视频 | 日韩免费在线观看视频 | 国产精品不卡一区二区三区 | 国产成人午夜高潮毛片 |