問(wèn)題描述
客戶給了我一項(xiàng)新任務(wù),主要是為演員/歌手等創(chuàng)建一個(gè) CMS,客戶將向他們出售.
I have been given a new task from the client which is basically creating a CMS for actors/singers and the like that client will be selling out to them.
它基本上是一個(gè)軟件包,開(kāi)箱即用,與 WordPress 非常相似,您只需將其交給購(gòu)買(mǎi)它的人,當(dāng)然這不會(huì)成為一個(gè)博客平臺(tái).它將允許開(kāi)發(fā)人員:
It will basically be a package and would work out-of-box pretty much similar to WordPress, you just hand over to whoever buys it but of course this is not going to be a blogging platform. It will allow developers to:
- 添加插件/小部件
- 添加模板/主題
我認(rèn)為 觀察者模式 可能有用,但我不太確定.你們可以建議在以下方面創(chuàng)建這種靈活/可擴(kuò)展的 CMS:
I thought the Observer Pattern may be useful but I am not that sure about it. What you guys could suggest to create such flexible/extensible CMS in terms of:
- 能夠添加插件(例如 WordPress)
- 能夠添加主題/模板(例如 WordPress)
- 設(shè)計(jì)模式
- 任何其他事情
推薦答案
Observer 很好,但您將不得不考慮超越基本模式.規(guī)范的觀察者/主題模式只將主題對(duì)象發(fā)送給觀察者,沒(méi)有別的,甚至沒(méi)有為什么通知它.
Observer's fine, but you're going to have to consider going beyond the basic pattern. The canonical Observer/Subject pattern only sends the Subject object to the Observer, nothing else, not even why it's being notified.
最初,解決方案似乎還包括向觀察者發(fā)出通知的原因,但隨后您可能最終會(huì)通知不關(guān)心某些通知的觀察者.更好的解決方案可能是要求觀察者還要求提供他們希望收到的通知列表.
Initially, the solution might seem like also including the reason for the notification to the Observer, but then you might end up notifying Observers that don't care about certain notifications. A better solution might be requiring Observers to also ask for a list of notifications they'd like to receive.
但這也帶來(lái)了一個(gè)問(wèn)題.為了讓觀察者真正將自己附加到主題,它們必須被實(shí)例化.每一次.即使他們永遠(yuǎn)不需要.那是愚蠢的.
But that also presents a problem. In order for the Observers to actually attach themselves to Subjects, they have to be instantiated. Every single time. Even if they'd never be needed. That's silly.
因此,我們很快就達(dá)到了插件的規(guī)范 PHP 實(shí)現(xiàn)之一:hooks".Hooks 使用與 Observer/Subject 相同的概念,但在一個(gè)非常重要的方面實(shí)現(xiàn)不同:實(shí)際的 Observers 不是為了觀察 Subjects 而實(shí)例化的.相反,Subjects 向某種中央存儲(chǔ)庫(kù)發(fā)送通知.該存儲(chǔ)庫(kù)配置了所有已安裝和激活插件(觀察者)的列表,并包含每個(gè)插件想要接收的所有事件的列表.每個(gè)插件僅在事件發(fā)生時(shí)通知,通常通過(guò)靜態(tài)方法而不是通過(guò)創(chuàng)建插件實(shí)例并通知它.call_user_func_array
和一個(gè)好的自動(dòng)加載器使這變得非常簡(jiǎn)單.
So, we've quickly reached one of the canonical PHP implementations of plugins: "hooks". Hooks use the same concept as Observer/Subject, but the implementation is different in a very important way: the actual Observers aren't instantiated in order to Observe Subjects. Instead, Subjects send a notification to some variety of central repository. This repository is configured with a list of all installed and activated plugins (Observers), and contains a list of all of the events that each plugin wants to receive. Each plugin and notified only when the event takes place, often through a static method rather than by creating an instance of the plugin and notifying it. call_user_func_array
and a good autoloader makes this incredibly trivial.
因此,您可以為所有插件創(chuàng)建一個(gè)簡(jiǎn)單的接口來(lái)實(shí)現(xiàn).您需要的方法包括但不限于:
You can therefore create a simple Interface for all plugins to implement. Methods that you'll need include but are not limited to:
- 用于獲取有關(guān)插件數(shù)據(jù)的內(nèi)容,例如名稱、作者、官方網(wǎng)站、版本等.人類可使用的信息.
- 返回插件想要訂閱的事件的方法.
- 一種安裝方法,用于插件為了安裝自身而需要做的事情,例如操作數(shù)據(jù)庫(kù).
- 卸載方法也可能很方便.
- 將接收事件通知并返回所需數(shù)據(jù)的(可能是靜態(tài)的)方法.
根據(jù)您采用插件概念的程度,您最終可能會(huì)得到具有用戶可配置選項(xiàng)的插件.您可能需要考慮到這一點(diǎn).沿著這條路走下去的是瘋狂和配置系統(tǒng).
Depending on how far you take the plugin concept, you could end up with plugins that have user configurable options. You might need to take that into account. Down that road lies madness and configuration systems.
為了使插件有效,您需要到處放置鉤子,并經(jīng)常與最終用戶合作,在需要的地方添加新的鉤子.
In order to make plugins effective, you're going to need to place hooks everywhere, and frequently work with end-users to add new hooks where they are needed.
小部件可以很容易地以類似的方式工作,就像在頁(yè)面呈現(xiàn)之前調(diào)用的插件一樣.
Widgets can easily work in a similar way, as plugins that get called prior to page rendering.
主題/模板,天哪.您可能有兩大選擇.
Themes/templates, oh my. You probably have two big options.
- Smarty 或類似的模板引擎.或者您自己的非 PHP 模板引擎.
- PHP 模板.
此決定將由您的最終用戶決定.Smarty 的限制令人難以置信,但如果您想確保模板中只運(yùn)行經(jīng)過(guò)批準(zhǔn)的代碼,它可能是一個(gè)可行的選擇.此外,允許直接在應(yīng)用程序本身中編輯 Smarty 模板并不是不安全的.
This decision will be driven by your end users. Smarty is incredibly limiting, but if you want to make sure that only approved code runs in a template, it might be a viable option. Furthermore, it's not unsafe to allow editing of Smarty templates right in the application itself.
另一方面,Wordpress 模板運(yùn)行良好的原因之一是它們是純 PHP 的.他們可以調(diào)用 Wordpress API 中公開(kāi)的任何方法,甚至可以執(zhí)行自己有趣的邏輯.如果您希望您的最終用戶具有技術(shù)頭腦,或者至少具有技術(shù)能力,那么 PHP 模板就是您的最佳選擇.另一方面,如果惡意用戶進(jìn)入管理位,允許在應(yīng)用程序中編輯 PHP 模板可能會(huì)打開(kāi)一個(gè)巨大的潛在安全漏洞.您可能想限制對(duì)文件系統(tǒng)的編輯.
On the other hand, one of the reason Wordpress templates work so well is that they're pure PHP. They can call any method exposed in the Wordpress API, and even do their own interesting logic. If you expect your end users to be technically minded, or at least technically competent, then PHP templates are the way to go. On the other hand, allowing editing of PHP templates within the application can open up a huge potential security hole if a malicious user gets into the admin bits. You probably want to restrict editing to the filesystem.
雖然這涵蓋了 HTML 創(chuàng)建,但您還應(yīng)該考慮 CSS.您的最終用戶能否直接操作 CSS?他們?cè)敢鈫?如果您的默認(rèn)模板包含足夠多的語(yǔ)義類,那么如果他們知道自己在做什么,他們可能會(huì)毫不費(fèi)力地進(jìn)行大量樣式設(shè)置.另一方面,您的最終用戶可能不知道 CSS 是什么,所以他們可能想要,哦,比如說(shuō),顏色選擇器和預(yù)先構(gòu)建的配色方案,以及配色方案選擇器,以及其他類似令人討厭的東西來(lái)構(gòu)建.現(xiàn)在最好考慮一下那些恐怖事件.
While this covers HTML creation, you should also take CSS into consideration. Will your end-users be able to manipulate CSS directly? Will they want to? If your default templates include enough semantic classes, they can probably do a great deal of styling with not a lot of effort, if they know what they're doing. On the other hand, your end-users might not know what CSS is, so they might want, oh, say, color pickers and pre-built color schemes, and a color scheme chooser, and other such annoying things to build. It's probably best to think about those horrors now.
雜項(xiàng).
如果沒(méi)有草稿和發(fā)布狀態(tài)的概念,任何 CMS 都是不完整的.除了先編碼之外,我在這里沒(méi)有給你任何建議.如果您的客戶或最終用戶想要任何類型的歷史存檔、管理審批機(jī)制或任何其他使草稿/發(fā)布的內(nèi)容不只是簡(jiǎn)單狀態(tài)字段的內(nèi)容,您需要很快知道.(我被這個(gè)深深地咬住了.我們圍繞一個(gè)簡(jiǎn)單的已發(fā)布/未發(fā)布的模型設(shè)計(jì)了整個(gè)系統(tǒng),并在我們意識(shí)到它不起作用時(shí)通過(guò)規(guī)范構(gòu)建和相關(guān)原型代碼獲得了大約 9/10我們必須做一些更遠(yuǎn)、更復(fù)雜的事情才能真正滿足客戶的要求.重建粗略的計(jì)劃是我們迄今為止遇到的最大的一次浪費(fèi)時(shí)間.)
No CMS would be complete without the concept of drafts and publish states. I don't have any advice for you here, other than code this first. If your customer or the end-users want any sort of historical archiving, managerial approval mechanism, or anything else that makes draft/published anything but a simple state field, you need to know very soon. (I've been bitten horribly by this one. We'd designed the entire system around a simple published/not-published model, and got about 9/10ths through spec building and related prototype code when we realized it wouldn't work and we'd have to do something far, far more complex to actually meet customer requirements. Rebuilding the rough plan was the single largest time-sink we encountered so far.)
你會(huì)使用 ORM 嗎?如果沒(méi)有,請(qǐng)確保使用適當(dāng)?shù)臄?shù)據(jù)庫(kù)接口庫(kù).PDO,或者來(lái)自 PEAR 的東西,或者 Zend_Db.您不可避免地會(huì)有一個(gè)客戶堅(jiān)持代碼在 Oracle 或 MSSQL 上運(yùn)行.或 SQLite.很高興告訴他們可以做到(需要付出一些努力).插件作者也會(huì)感謝您的理智.不要自己動(dòng)手.
Will you use an ORM? If not, be sure to use a proper database interface library. PDO, or maybe something from PEAR, or maybe Zend_Db. You'll inevitably have a customer that will insist that the code runs on Oracle or MSSQL. Or SQLite. It'll be nice to tell them it can be done (with some effort). Plugin authors will thank you for the sanity as well. Don't roll your own.
(再說(shuō)一次,對(duì)于您的代表級(jí)別,我希望您已經(jīng)熟悉我所說(shuō)的幾乎所有內(nèi)容.啊,我做的事情是在思考自己的編碼問(wèn)題時(shí)分散自己的注意力......)
(Then again, with your rep level, I expect that you're already familiar with pretty much everything I've said. Ah, the things I do to distract myself while thinking about my own set of coding problems...)
這篇關(guān)于PHP:創(chuàng)建可擴(kuò)展的 CMS 系統(tǒng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!