開發一個 Model-View-Controller 元件/新增更新伺服器

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 an update server and the translation is 17% complete.

Other languages:
العربية • ‎Deutsch • ‎English • ‎español • ‎فارسی • ‎français • ‎Nederlands • ‎中文(台灣)‎
開發一個 Model-View-Controller 元件

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


In this part, we will cover the various aspects of setting up an update server for our helloworld component.

A video accompanying this step is available at Adding an Update Server.


Updating Joomla extensions via an update server is a two step process:

  1. finding the updates available for the installed extensions
  2. selecting an update and installing it.

Typically this is done by the administrator via the Extensions / Manage / Update page.

  1. You click on Find Updates to find the updates available
  2. You mark the checkbox of each update you want to install and click on Update.

In this step we enable our Helloworld component to be updated in this way.

Finding Updates

Each Joomla extension which is updated via an update server has a URL associated with it which points to an XML file containing details of the updates available. This file could be hosted on the extension developer's own website, for example. If you go to Extensions / Manage / Update Sites you will see this URL in the Update Site column, and you can click on those URLs to see the format of the XML files. (Some of these XML files may be of type "collection" where the first XML element is <extensionset>, and these are described towards the end of this tutorial, but for now we're concentrating on the type "extension" where the first XML element is <updates>).

The XML file (described more fully below) contains details of what updates are available for that extension, including any restrictions (eg you have to be running a certain Joomla version to be able to use it), and the URL of the zip file containing the new version of the extension. You tell Joomla the URL of the XML file for your extension within the manifest file (helloworld.xml), eg

	<server type="extension" name="Helloworld Updates">http://myDeveloperSite/helloworld-updates/updates.xml</server>

Below is a basic update XML file

		<name>Helloworld component</name>
		<description>Helloworld component for Joomla MVC tutorial</description>
			<downloadurl type="full" format="zip">http://myDeveloperSite/helloworld-updates/helloworld-1-0-0.zip</downloadurl>
		<targetplatform name="joomla" version="3.[23456789]" />

Note that it has

  • basic information about the component – eg name, version
  • a link to the zip file for that update
  • restrictions – for Joomla versions 3.2 to 3.9 only, and at least PHP version 7.1

Whenever an administrator clicks on the Find Updates button, Joomla sends HTTP requests to each of the URLs of these update servers to retrieve the XML files. It parses each XML file,

  1. ensures it's properly formed XML
  2. compares the version of the update with the version of the extension currently running
  3. checks any restrictions (Joomla versions, PHP versions, database versions) with what is currently running.

If it finds it's a valid update for this website, then it inserts a record into the "updates" table in the database. (You'll find at most 1 update for each extension, because if there are several possible then Joomla will store only the most recent valid version, ie with the highest version number).

Installing Updates

Once an update has been inserted into the "updates" table it is visible to the administrator in the Extension / Manage / Update page. After checking the checkbox and clicking the Update button Joomla will

  • once again send an HTTP request to the URL of the update XML file
  • parse the XML file and ensure that conditions for installing that version are still met
  • send an HTTP request to the URL of the update zip file, storing the file received in the HTTP response in its /tmp directory
  • proceed to install the new version from the downloaded zip file.


In this tutorial step we will update our helloworld extension to use an update server. We'll also set up the server files so that you can verify that the update process works ok. To do this you'll need to configure part of your development area to act as the update server. This basically means creating a directory below the root directory serviced by your web server, and including the update XML file etc in it. For example, I have a folder /helloworld which contains my Joomla instance, so that to access the site I put the URL http://localhost/helloworld/index.php into the browser. For the update server I created a folder /helloworld-updates at the same level as /helloworld, and put the updates.xml file into that folder. I can then access the XML file by putting the URL http://localhost/helloworld-updates/updates.xml into the browser. You'll need to do something similar in your own environment, and verify that the URL is correct by accessing the XML file through your browser.

Updated Code

The only change we need to make to our helloworld component is to include the update server inside the manifest file. Once updated, zip up the component as usual and install it on your Joomla instance.


<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">

	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2018</creationDate>
	<author>John Doe</author>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<!-- The description is optional and defaults to the name -->

	<!-- Runs on install/uninstall/update; New in 2.5 -->

	<install> <!-- Runs on install -->
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
	<uninstall> <!-- Runs on uninstall -->
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
	<update> <!-- Runs on update; New since J2.5 -->
			<schemapath type="mysql">sql/updates/mysql</schemapath>

	<!-- 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">

		<languages folder="site/language">
			<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
			<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>

	<media destination="com_helloworld" folder="media">

		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</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 -->
			<!-- SQL files section -->
			<!-- tables files section -->
			<!-- models files section -->
			<!-- views files section -->
			<!-- controllers files section -->
			<!-- helpers files section -->
			<!-- layout files section -->
		<languages folder="admin/language">
			<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
			<language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
			<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>
			<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.sys.ini</language>

		<!-- Change the URL below to match that of your own update XML file -->
		<server type="extension" name="Helloworld Updates">http://localhost/helloworld-updates/updates.xml</server>

Update Server Files

We're going to put 3 files into the directory which we're using to act as the update server, ie into the /helloworld-updates folder, or wherever you have decided to use. The 3 files are:

  1. the updates.xml file
  2. an html file which is an information file for the latest version
  3. a zip file for the update.

For security purposes you should also add the usual blank index.html file as well. The updates.xml file is below. You should change the infourl and downloadurl to suit your environment, and set values for the targetplatform, php_minimum and supported_databases as you wish.

		<name>Helloworld component</name>
		<description>Helloworld component for Joomla MVC tutorial</description>
			<downloadurl type="full" format="zip">http://localhost/helloworld-updates/helloworld-1-0-0.zip</downloadurl>
		<targetplatform name="joomla" version="3.[23456789]" />
		<supported_databases mysql="5.6.19"></supported_databases>

You can find definitions of the elements of this XML structure at Deploying an Update Server, but note that the attributes min_dev_level and max_dev_level are marked as deprecated in the current (3.9) Joomla code, to be removed in Joomla 4. The information file is just a basic html document, which you can modify as you wish. Its filename must match what is specified in the <infourl> tag above.

<!DOCTYPE html>
	<title>Helloworld update</title>
	<h1>Update to version 1.0.0</h1>

Finally create a store a zip file for the new version. As a minimum you need to update the version in the manifest file, and you may wish to make another change which is visible when you visit the site.

  1. Update the manifest file (helloworld.xml) to set the version line to <version>1.0.0</version>
  2. Create a zip file of the helloworld component
  3. Copy the zip file into your update server directory. Its name must match what is specified in the <downloadurl> tag (helloworld-1-0-0.zip above).

Testing the Update Server

To test your update server do the following:

  1. log in to the backend of your site using an admin account
  2. go to Extensions / Manage / Update
  3. click on Find Updates – your helloworld component should appear as an update. (It may already be shown, because when you display the admin control panel it issues an ajax request which initiates the find updates process).
  4. click on the checkbox next to the helloworld component, and click on Update
  5. go to Extensions / Manage / Manage and find the helloworld component. Its version should now be 1.0.0 (or whatever version you updated to).

(For info, using phpmyadmin you can change the existing version number of the helloworld component by finding the appropriate record in the extensions table, and updating the version number within the json string in the manifest_cache field.)

Collections type

Up to now we've been considering the server type="extension", but Joomla also has a server type="collection". The purpose of this type is to allow you to have a collection of different update XML files. You would use a "collection" type if, for example,

  1. your extension is a package and you want to allow users to update the package components individually
  2. you want to have different update XML files for different versions of Joomla.

Joomla languages use a collection type. If you have several languages installed and go to Extensions / Manage / Update Sites then you can see that all the languages have the same update site URL pointing to something like https://update.joomla.org/language/translationlist_3.xml . If you open that XML file then you see all the details URLs which are the update XML files of the individual languages, and which can be installed via the Extensions / Manage / Install Languages page.

Joomla core also uses a collection type. If you go to Update Sites and click on the XML file for Joomla! Core then you will see different update XML files depending upon whether the installed version of Joomla is 2.x or 3.x. If you wish to try using a collection type, then this is still possible with a single update XML file, by following the steps below. Firstly delete manually (using phpmyadmin) the records in the database tables update_sites and update_sites_extensions which relate to the helloworld component. Then go into Extensions / Manage / Update and click on Clear Cache. This should have removed all trace of the type="extension" update mechanism. Secondly replace the updateservers section of the manifest file with the following (changing the URL appropriately)

	<server type="collection" name="Helloworld Updates">http://localhost/helloworld-updates/list.xml</server>

Zip up the component and install it in the usual way. (Remember, when you install it in this way the version number in the new install file doesn't need to be greater than the version of the one currently installed). Thirdly create a list.xml file which points to the update XML file, and store this in your update server folder. Once again, change the URL and version to match your own situation, making sure that the version quoted in the file is greater than the one currently installed.

<extensionset name="Helloworld Update Collection" description="Helloworld Update as Collection">
	<extension name="Helloworld Component" element="com_helloworld" type="component" client="administrator" version="1.0.0" targetplatformversion="3.[23456789]" detailsurl="http://localhost/helloworld-updates/updates.xml"/>

If you test the update process as above you should find it works just the same. However, note that in this case the updates.xml file is not processed during the Find Updates process, so if you have an incompatible minimum value of PHP say, then it won't report it at that stage.