開發一個 Model-View-Controller 元件/新增一個選單類型到前台
From Joomla! Documentation
< J3.x:Developing an MVC Component
在選單類型中新增一個變數請求
使用資料庫
基本的後台
新增語言管理
新增後台行為
新增後台的佈置
新增表單驗證
新增類別
新增設定介面
新增一個安裝/移除/更新程式碼檔案
新增一個前台表單
使用語言過濾功能
新增更新伺服器
這是一系列的多篇文章,旨在介紹如何開發一個 Joomla! Version Model-View-Controller 元件
讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。
注意
- 本教學是 開發Joomla! 3.x MVC 元件 系列文章的一部分,閱讀本文之前,您應該先看過前面幾篇文章。
- You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the archive
- You can watch a video associated with this step in the tutorial at Step 3, Adding a Menu Part.
把 Hello World 加到選單項目
In this article we will cover how to add a menu item 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 menu item 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 | 建立: default.xml | <path_to_com_helloworld/site/views/helloworld/tmpl/default.xml |
2 | 更新: helloworld.xml | <path_to_com_helloworld/helloworld.xml |
更新 Hello World! 元件
To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.
After the component has been updated, we will be able to add a Menu Item to it. This will allow us to access our component a menu rather than having to remember what to type into the address bar. We'll do this using the Menu Manager of Joomla!.
- Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <yoursite>/joomla/administrator/index.php. For this example we will navigate to localhost/joomla/administrator/index.php.
- Add a new menu item by clicking on Menu → <existing menu> → Add New Menu Item. A new screen will appear.
- In the "Menu Title" field, enter "Hello World!"
- In the "Menu Type" field, click on the "Select" button and choose Hello World! → Hello World from the selection screen.
- 點擊「儲存並關閉」
You should now be able to access the component through the menu that you just created.
檔案細節
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>
</metadata>
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.3</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>
</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>
程式碼解說
為了滿足好奇的您,這裡說明他們是如何運作的。
default.xml
Note - For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.
helloworld.xml
<version>0.0.3</version>
更新版本號
元件內容
一路走到教學的這個步驟,您的元件應該包含以下檔案:
1 | helloworld.xml | this is an XML (manifest) file that tells Joomla! how to install our component. |
2 | site/helloworld.php | this is the site entry point to the Hello World! component |
3 | site/index.html | 避免網站伺服器列出路徑檔案內容 |
4 | site/controller.php | file representing the controller |
5 | site/views/index.html | 避免網站伺服器列出路徑檔案內容 |
6 | site/views/helloworld/index.html | 避免網站伺服器列出路徑檔案內容 |
7 | site/views/helloworld/view.html.php | file representing the view |
8 | site/views/helloworld/tmpl/index.html | 避免網站伺服器列出路徑檔案內容 |
9 | site/views/helloworld/tmpl/default.php | 預設的 view |
10 | site/views/helloworld/tmpl/default.xml | file that adds menu item |
11 | admin/index.html | 避免網站伺服器列出路徑檔案內容 |
12 | admin/helloworld.php | this is the administrator entry point to the Hello World! component |
13 | admin/sql/index.html | 避免網站伺服器列出路徑檔案內容 |
14 | admin/sql/updates/index.html | 避免網站伺服器列出路徑檔案內容 |
15 | admin/sql/updates/mysql/index.html | 避免網站伺服器列出路徑檔案內容 |
16 | admin/sql/updates/mysql/0.0.1.sql | file allowing to initialise schema version of the com_helloworld component. |