開發 MVC 元件/在選單類型中新增一個變數請求
From Joomla! Documentation
< J3.x:Developing an MVC Component
在選單類型中新增一個變數請求
使用資料庫
基本的後台
新增語言管理
新增後台行為
新增後台的佈置
新增表單驗證
新增類別
新增設定介面
新增一個安裝/移除/更新程式碼檔案
新增一個前台表單
使用語言過濾功能
新增更新伺服器
這是一系列的多篇文章,旨在介紹如何開發一個 Joomla! Version Model-View-Controller 元件
讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。
介紹
- 本教學是 開發Joomla! 3.x MVC 元件 系列文章的一部分,閱讀本文之前,您應該先看過前面幾篇文章。
- 您可以觀賞和此步驟相關的影片教學 Step 5. 在選單類型中新增一個變數請求.
在選單類型中新增一個變數請求
到了此時,顯示的訊息總是一成不變的 Hello World!。從 Joomla! 2.5 開始,您可以加入變數到選單類型。以我們的案例來說,是在site/views/helloworld/tmpl/default.xml 檔案中完成:
site/views/helloworld/tmpl/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
</layout>
<fields name="request">
<fieldset name="request">
<field
name="id"
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
default="1">
<option value="1">Hello World!</option>
<option value="2">Good bye World!</option>
</field>
</fieldset>
</fields>
</metadata>
我們要將類型 list 欄位納入這個選單。這是 Joomla 標準表單欄位類型(Standard Form Field Types) 之一
兩件重要的事情要留意:
- 欄位群組中的 request 表示該欄位必填。
- the name attribute can be passed to the component in the URL. In this case ?option=com_helloworld&id=1 would indicate that you had chosen option 1.
The model has to be modified in order to switch between the two different messages (depending upon which is chosen by the administrator with the field defined above):
site/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var string message
*/
protected $message;
/**
* Get the message
* @return string The message to be displayed to the user
*/
public function getMsg()
{
if (!isset($this->message))
{
$jinput = JFactory::getApplication()->input;
$id = $jinput->get('id', 1, 'INT');
switch ($id)
{
case 2:
$this->message = 'Good bye World!';
break;
default:
case 1:
$this->message = 'Hello World!';
break;
}
}
return $this->message;
}
}
也請修改您的helloworld.xml檔案為更新版本:
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.5</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>
</extension>
您可以在瀏覽器網址列中輸入 index.php?option=com_helloworld&view=helloworld&id=1 或是 index.php?option=com_helloworld&view=helloworld&id=2,測試這個版本的請求
包裝元件
您的元件路徑中,會有以下內容:
- helloworld.xml
- site/helloworld.php
- site/index.html
- site/controller.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/models/index.html
- site/models/helloworld.php
- admin/index.html
- admin/helloworld.php
- admin/sql/index.html
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
壓縮這個資料夾路徑或是直接下載archive 接著使用 Joomla! 擴充套件管理員安裝。您可以在後台選單轉裡的地方,新增這個元件的選單項目。
請建立一個PR(pull request ),或是到此 https://github.com/joomla/Joomla-3.2-Hello-World-Component 建立一個issue,如果本頁有什麼程式碼方面的差異或是編輯的需要。