J3.x

開發 MVC 元件/新增一個 model 到前台

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 model to the site part and the translation is 79% complete.

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


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

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



注意

  • You can follow the steps below to add a model to the Hello World! component, or you can directly download the archive

在 Hello World! 元件中加 Model

In this article we will cover how to add a model to a basic Joomla! component. For this example we will be continuing our work on the Hello World! component.

There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.

1 手動新增檔案到 <path_to_joomla>/
2 Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install
3 Update using the Joomla! Extension Manager and an Update Server

To add a model you will need to navigate to com_helloworld, which is the original directory we made for our component. You must use the updated directory structure from the last tutorial. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in File Details.

1 建立: helloworld.php <path_to_com_helloworld>/site/models/helloworld.php
2 建立: index.html <path_to_com_helloworld>/site/models/index.html
3 更新: view.html.php <path_to_com_helloworld>/site/views/helloworld/view.html.php
4 更新: helloworld.xml <path_to_com_helloworld>/helloworld.xml

更新 Hello World! 元件

要在 Joomla! 中更新 Hello World! 元件,請參考以下步驟original installation

檔案細節

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))
		{
			$this->message = 'Hello World!';
		}

		return $this->message;
	}
}

site/views/helloworld/view.html.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');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld extends JViewLegacy
{
	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		// Assign data to the view
		$this->msg = $this->get('Msg');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

			return false;
		}

		// Display the view
		parent::display($tpl);
	}
}

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.4</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>


程式碼解說

為了滿足好奇的您,這裡說明他們是如何運作的。

helloworld.php

class HelloWorldModelHelloWorld extends JModelItem
{

This class will be called by the class HelloWorldViewHelloWorld.

view.html.php

$this->msg = $this->get('Msg');

The HelloWorldViewHelloWorld class asks the model for data using the get method of the JViewLegacy. This get method converts the get('Msg') call into a getMsg() call on the model, which is the function which we have had to provide.

元件內容

一路走到教學的這個步驟,您的元件應該包含以下檔案:

1 helloworld.xml this is an XML (manifest) file that tells Joomla! how to install our component.
2 site/helloworld.php 這是前台使用者訪問 Hello World! 元件的入口
3 site/index.html 避免網站伺服器列出路徑檔案內容
4 site/controller.php 呈現 controller 的檔案
5 site/models/helloworld.php 呈現 model 的檔案
6 site/models/index.html 避免網站伺服器列出路徑檔案內容
7 site/views/index.html 避免網站伺服器列出路徑檔案內容
8 site/views/helloworld/index.html 避免網站伺服器列出路徑檔案內容
9 site/views/helloworld/view.html.php 呈現 view 的檔案
10 site/views/helloworld/tmpl/index.html 避免網站伺服器列出路徑檔案內容
11 site/views/helloworld/tmpl/default.php 預設的 view
12 site/views/helloworld/tmpl/default.xml 新增選單項目的檔案
13 admin/index.html 避免網站伺服器列出路徑檔案內容
14 admin/helloworld.php 這是系統管理員進入Hello World! 元件的入口
15 admin/sql/index.html 避免網站伺服器列出路徑檔案內容
16 admin/sql/updates/index.html 避免網站伺服器列出路徑檔案內容
17 admin/sql/updates/mysql/index.html 避免網站伺服器列出路徑檔案內容
18 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.
Info non-talk.png
General Information

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