J3.x

Developing an MVC Component/Adding a menu type to the site part

From Joomla! Documentation

< J3.x:Developing an MVC Component
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
Tutorial
Developing an MVC Component



This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).



Note[edit]

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

Adding a Menu Item to Hello World[edit]

In this article we will cover how to add a view 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 Manually add the files into <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

In the Joomla! framework, components are executed using menu items. These items are links on your Joomla! page, that open a specific page of your component. To add a menu item you will need to:

1 Create: default.xml <path_to_joomla>htdocs/components/com_helloworld/views/helloworld/tmpl/default.xml
2 Update: helloworld.xml <path_to_joomla>/htdocs/components/com_helloworld/helloworld.xml

Create default.xml[edit]

Using your preferred file editor, create <path_to_joomla>htdocs/components/com_helloworld/views/helloworld/tmpl/default.xml and include the source code as found File Details.

Code Explanation: For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.

Update helloworld.xml[edit]

Using your preferred file editor, open <path_to_joomla>/htdocs/components/com_helloworld/helloworld.xml and include the revised source code as found File Details.

Code Explanation:

<version>0.0.3</version>

Updates the version number

File Details[edit]

site/views/helloworld/tml/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.2.0" method="upgrade">

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2014</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>

Component Contents[edit]

At this point in the tutorial, your component should contain the following files:

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 prevents web server from listing directory content
4 site/controller.php file representing the controller
5 site/views/index.html prevents web server from listing directory content
6 site/views/helloworld/index.html prevents web server from listing directory content
7 site/views/helloworld/view.html.php file representing the view
8 site/views/helloworld/tmpl/index.html prevents web server from listing directory content
9 site/views/helloworld/tmpl/default.php the default view
10 site/views/helloworld/tmpl/default.xml file that adds menu item
11 admin/index.html prevents web server from listing directory content
12 admin/helloworld.php this is the administrator entry point to the Hello World! component
13 admin/sql/index.html prevents web server from listing directory content
14 admin/sql/updates/index.html prevents web server from listing directory content
15 admin/sql/updates/mysql/index.html prevents web server from listing directory content
16 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.
You can add a menu item of this component using the menu manager in the backend. To do so select "Add New Menu Item" from the one of the menus in the "Menus" menu; then you can select COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE for Menu Item Type. Once selected you can see the Link information is populated with the URL for the view.

Contributors[edit]