J3.x

開發 MVC 元件/在選單類型中新增一個變數請求

From Joomla! Documentation

< J3.x:Developing an MVC Component
This page is a translated version of the page J3.x:Developing an MVC Component/Adding a variable request in the menu type and the translation is 90% complete.

Other languages:
العربية • ‎English • ‎español • ‎français • ‎Nederlands • ‎中文(台灣)‎
Joomla! 
3.x
教學
開發一個 Model-View-Controller 元件


這是一系列的多篇文章,旨在介紹如何開發一個 Joomla! VersionJoomla 3.x Model-View-Controller 元件

讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。



介紹

在選單類型中新增一個變數請求

到了此時,顯示的訊息總是一成不變的 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,測試這個版本的請求

包裝元件

您的元件路徑中,會有以下內容:

壓縮這個資料夾路徑或是直接下載archive 接著使用 Joomla! 擴充套件管理員安裝。您可以在後台選單轉裡的地方,新增這個元件的選單項目。

Info non-talk.png
General Information

請建立一個PR(pull request ),或是到此 https://github.com/joomla/Joomla-3.2-Hello-World-Component 建立一個issue,如果本頁有什麼程式碼方面的差異或是編輯的需要。

貢獻者