php中logic(),PHP中表示和業務邏輯的分離 - Separation of presentation and business logic in PHP - 开发者知识库...

I am programming my first real PHP website and am wondering how to make my code more readable to myself. The reference book I am using is PHP and MySQL Web Development 4th ed.

我正在為我的第一個真正的PHP網站編程,我想知道如何使我的代碼對我自己更具可讀性。我正在使用的參考書是PHP和MySQL Web開發第4版。

The aforementioned book gives three approaches to separating logic and content:

前面提到的書給出了將邏輯和內容分開的三種方法:

include files

包含文件

function or class API

函數或類的API

template system

模板系統

I haven't chosen any of these yet, as wrapping my brains around these concepts is taking some time. However, my code has become some hybrid of the first two as I am just copy-pasting away here and modifying as I go.

我還沒有選擇其中的任何一個,因為圍繞這些概念進行思考需要一些時間。但是,我的代碼已經變成了前兩個的混合,因為我只是在這里復制粘貼,然后修改。

On presentation side, all of my pages have these common elements: header, top navigation, sidebar navigation, content, right sidebar and footer.

在演示方面,我的所有頁面都有這些常見元素:header、top導航、側邊欄導航、內容、右側側邊欄和頁腳。

The function-based examples in the book suggest that I could have these display functions that handle all the presentation example. So, my page code will be like this:

書中基於函數的示例表明,我可以使用這些顯示函數來處理所有的表示示例。我的頁面代碼是這樣的:

display_header();

display_navigation();

display_content();

display_footer();

However, I don't like this because the examples in the book have these print statements with HTML and PHP mixed up like this:

但是,我不喜歡這樣,因為書上的例子有這樣的print語句,HTML和PHP是這樣混合的:

echo "

" ...

I would rather like to have HTML with some PHP in the middle, not the other way round.

我更喜歡中間有一些PHP的HTML,而不是相反。

I am thinking of making my pages so that at the beginning of my page, I will fetch all the data from database and put it in arrays. I will also get the data for variables. If there are any errors in any of these processes, I will put them into error strings.

我正在考慮創建頁面,以便在頁面的開始,我將從數據庫中獲取所有數據並將其放入數組中。我也會得到變量的數據。如果這些進程中有任何錯誤,我將把它們放入錯誤字符串中。

Then, at the HTML code, I will loop through these arrays using foreach and display the content. In some cases, there will be some variables that will be shown. If there is an error variable that is set, I will display that at the proper position.

然后,在HTML代碼中,我將使用foreach循環這些數組並顯示內容。在某些情況下,會顯示一些變量。如果有一個錯誤變量被設置,我將顯示在正確的位置。

(As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

(順便說一句:我不理解的是,在大多數示例代碼中,如果某個數據庫查詢或什么的出現錯誤,則總是:

else echo 'Error';

This baffles me, because when the example code gives an error, it is sometimes echoed out even before the HTML has started...)

這讓我感到困惑,因為當示例代碼出現錯誤時,它有時甚至在HTML開始之前就被回顯……

For people who have used ASP.NET, I have gotten somewhat used to the code-behind files and lblError and I am trying to do something similar here.

對於使用過ASP的人。NET,我已經習慣了代碼隱藏文件和lblError,我正在嘗試做一些類似的事情。

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

我還沒搞清楚的是,我怎么能做到"先做邏輯,再做表示"這樣我就不用在所有頁面中復制導航邏輯和導航表示了。

Should I do some include files or could I use functions here but a little bit differently? Are there any good articles where these "styles" of separating presentation and logic are explained a little bit more thoroughly. The book I have only has one paragraph about this stuff.

我應該做一些包含文件還是我可以在這里使用函數但是有一點不同?是否有一些好的文章詳細地介紹了這些分離表示和邏輯的“風格”。關於這件事,我的書只有一段。

What I am thinking is that I am talking about some concepts or ways of doing PHP programming here, but I just don't know the terms for them yet.

我想說的是,我在這里講的是PHP編程的一些概念或方法,但我還不知道它們的術語。

I know this isn't a straight forward question, I just need some help in organizing my thoughts.

我知道這不是一個直截了當的問題,我只是需要一些幫助來組織我的思想。

3 个解决方案

#1

4

Never echo out HTML with PHP. Instead write it inline (without evil short tags) as

永遠不要用PHP回顯HTML。相反,把它寫在內聯的(沒有惡意的短標簽)as上

< tr類= " < ?php echo $ myclass;? > " >

Other options for helping to separate out the logic / view would be to use a PHP Framework like CodeIgniter.

幫助分離邏輯/視圖的其他選項是使用像CodeIgniter這樣的PHP框架。

I would ditch the book and instead focus more on learning core PHP skills like functions, classes, etc. Then start playing the the several popular frameworks out there.

我將放棄這本書,轉而專注於學習核心PHP技能,如函數、類等,然后開始播放一些流行的框架。

As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

順便說一下:我不理解的是,在大多數示例代碼中,如果某些數據庫查詢或什么不給出錯誤,總是有:

That's because they are displaying the errors incorrectly. You should either store the errors in a sesssion and then display them on the page (clearing them as well) or throw them into the error log with the error_log function. error_log("Something happened in MyClass");

那是因為他們顯示錯誤了。您應該將錯誤存儲在sesssion中,然后在頁面上顯示它們(也清除它們),或者使用error_log函數將它們放入錯誤日志中。error_log(“MyClass發生了一件事”);

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

我還沒搞清楚的是,我怎么能做到"先做邏輯,再做表示"這樣我就不用在所有頁面中復制導航邏輯和導航表示了。

Think of things in a MVC approach. You call the controller (logic) first. It figures out what is needed. If it needs data from the database it invokes a Model and requests it. Then it formats it, adds other data, runs additional queries, and then passes it to the view.

用MVC方法考慮問題。首先調用控制器(邏輯)。它弄清楚了需要什么。如果它需要來自數據庫的數據,它將調用一個模型並請求它。然后將其格式化,添加其他數據,運行額外的查詢,然后將其傳遞給視圖。

#2

1

sound like a template engine is what you are looking for - ask google for a lot of results. personally, i like smarty very much.

聽起來像一個模板引擎是你正在尋找的-問谷歌的很多結果。就我個人而言,我非常喜歡smarty。

(and throw away that book, sound like it's... old)

把那本書扔掉,聽起來好像……老)

#3

1

MVC (Model View Controller) sounds like it might suit your needs. You can read about that here.

MVC(模型視圖控制器)聽起來可能適合您的需要。你可以在這里讀到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值