開發一個 MVC 元件
From Joomla! Documentation
< J3.x:Developing an MVC Component
在選單類型中新增一個變數請求
使用資料庫
基本的後台
新增語言管理
新增後台行為
新增後台的佈置
新增表單驗證
新增類別
新增設定介面
新增一個安裝/移除/更新程式碼檔案
新增一個前台表單
使用語言過濾功能
新增更新伺服器
這是一系列的多篇文章,旨在介紹如何開發一個 Joomla! Version Model-View-Controller 元件
讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。
注意
- 假如您初來乍到 Joomla!,請先閱讀元件如何運作的第一課。
- 這一個教學是從 Christophe Demko: Developing a Model-View-Controller Component/2.5 引用出來的。
- 警告: 這一篇教學沒有重複 Demko 原始文章中的內容,要查看它們,請到 Joomla! 2.5 原始的教學內容:Developing an MVC Component for Joomla! 2.5 - Introduction
需求
您需要 Joomla! 3.0 (PHP, MySQL 以及 Apache/Microsoft IIS) 或更高版本,來符合本教學。 我收集了很多資訊,接著開始將元件從2.5移轉為3.0。 以下是一些和移轉元件有關的重要資訊,也請閱讀其他所有的 升級版本文章。 使用 "display_errors On" 來協助除錯。
從 Joomla! 2.5 移轉到 Joomla! 3.0:
請記得您需要在每一個您想要直接擴展 JModel
, JView
或是 JController
的地方新增 legacy 。如果非直接 (例如透過 JModelList) 則您不需要這樣做,程式本身已經有準備好了。
另外一項影響是,我們講了很久的,過時程式碼被移除了 (我猜 JParameter 會是最大的衝擊所在),擴充套件應該只需要些微修改 ... 雖然您會想要看修改之後的樣子, Kyle 正在做這一部分。
當然,假如您正在建立獨立運作的應用程序,新的 MVC 以及JApplicationWeb/JApplicationCLI
絕對是您需要使用的。關於我們所完成的新程式包已經伴隨CMS可以取用。
(Elin in development list)
範例: DS Since we've removed the DS constant in 3.0, we need to replace the uses of the constant in com_media. The most unobtrusive change is to simply replace it with PHP's DIRECTORY_SEPARATOR constant since DS is an alias to that. However, the recommended way to do it is to simply use the slash, i.e. 'components/com_example/models/example.php' instead of 'components'.DS.'com_example'.DS.'models'.DS.'example.php'. This is windows safe.
(joomlacode)
if(!defined('DS')){
define('DS',DIRECTORY_SEPARATOR);
}
//$controller = JController::getInstance('HelloWorld');
$controller = JControllerLegacy::getInstance('HelloWorld');
//class HelloWorldViewHelloWorlds extends JView
class HelloWorldViewHelloWorlds extends JViewLegacy
class HelloWorldController extends JControllerLegacy
class HelloWorldModelHelloWorld extends JModelItemLegacy
class HelloWorldModelUpdHelloWorld extends JModelFormLegacy
JRegistry::getValue() now is JRegistry::get()
//Convert sample to JRegistry with LoadJSON - Sample from Joomla! 3.0 sourcecode
// $params = new JRegistry;
// $params->loadJSON($this->item->params);
// $this->item->params = $params;
$params = new JRegistry;
$params->loadString($item->params);
$item->params = $params;
這裡是原始碼: http://joomlacode.org/gf/project/hellojoomla3/frs/
Joomla! 3.0 新的 MVC
"Version 12.1 平台導入了新的 model-view-controller 範例格式。原則上,名為 JModel
, JView
和 JController
的class 現在是介面(interface),其基本 abstract classes 改為 JModelBase
, J ViewBase
和 JControllerBase
。此外,所有的 class 都被簡化,移掉了很多與Joomla CMS相耦合的內容,因為那對於獨立運作的 Joomla! Platform 應用程序來說並不需要" ...
Joomla! Platform Manual MVC