J3.x

Difference between revisions of "Developing an MVC Component/Adding a menu type to the site part"

From Joomla! Documentation

< J3.x:Developing an MVC Component
m (tml needs to be tmpl)
(13 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 
en</translate>}}
 
en</translate>}}
<translate>
 
  
==Note==
+
<translate>==Note== <!--T:19--></translate>
* This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
+
<translate><!--T:20-->
 +
* This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
  
* You can follow the steps below to add a view to the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-3-adding-a-site-menu.zip archive]
+
<translate><!--T:21-->
 +
* You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-3-adding-a-site-menu.zip archive]</translate>
  
== Adding a Menu Item to Hello World ==
+
<translate>
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 [[J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.
+
== Adding a Menu Item to Hello World == <!--T:22-->
 +
</translate>
 +
<translate><!--T:23-->
 +
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 [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.</translate>
  
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.
+
<translate><!--T:24-->
 +
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.</translate>
 
{| border=1
 
{| border=1
 
  | 1
 
  | 1
  | Manually add the files into <tt><path_to_joomla>/<tt>
+
  | <translate><!--T:25-->
 +
Manually add the files into</translate> <tt><path_to_joomla>/</tt>
 
  |-
 
  |-
 
  | 2
 
  | 2
  | Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install
+
  | <translate><!--T:26-->
 +
Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install</translate>
 
  |-
 
  |-
 
  |3
 
  |3
  | Update using the Joomla! Extension Manager and an [[Deploying_an_Update_Server| Update Server]]
+
  | <translate><!--T:27-->
 +
Update using the Joomla! Extension Manager and an [[S:MyLanguage/Deploying_an_Update_Server| Update Server]]</translate>
 
  |}
 
  |}
  
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:
+
<translate><!--T:28-->
 +
To add a menu item you will need to navigate to <tt>com_helloworld</tt>, which is the original directory we made for our component. You must use the updated directory structure from the last [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part | 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|File Details]].</translate>
  
 
{| border=1
 
{| border=1
 
  | 1
 
  | 1
  | Create: [[#site/views/helloworld/tmpl/default.xml| default.xml]]
+
  | <translate><!--T:29-->
  | <tt><path_to_joomla>htdocs/components/com_helloworld/views/helloworld/tmpl/default.xml<tt>
+
Create:</translate> [[#site/views/helloworld/tmpl/default.xml| default.xml]]
 +
  | <tt><path_to_com_helloworld/site/views/helloworld/tmpl/default.xml<tt>
 
  |-
 
  |-
 
  | 2
 
  | 2
  | Update: [[#helloworld.xml| helloworld.xml]]
+
  | <translate><!--T:30-->
  | <tt><path_to_joomla>/htdocs/components/com_helloworld/helloworld.xml</tt>
+
Update:</translate> [[#helloworld.xml| helloworld.xml]]
 +
  | <tt><path_to_com_helloworld/helloworld.xml</tt>
 
  |}
 
  |}
  
=== Create default.xml ===
+
<translate>
Using your preferred file editor, create <tt><path_to_joomla>htdocs/components/com_helloworld/views/helloworld/tmpl/default.xml</tt> and include the source code as found ''[[#File Details| File Details]]''.  
+
=== Updating the Hello World! Component === <!--T:31-->
 +
</translate>
 +
<translate><!--T:32-->
 +
To update the Hello World! Component in the Joomla! website, please follow the same steps for the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component#Installing the Hello World! Component |original installation]].</translate>
  
Code Explanation:
+
<translate><!--T:33-->
For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.
+
With the component successfully updated, we need to add the Menu Item. This will be done by using the Menu Manager of Joomla!.</translate>
  
===Update helloworld.xml===
+
<translate><!--T:34-->
Using your preferred file editor, open <tt><path_to_joomla>/htdocs/components/com_helloworld/helloworld.xml</tt> and include the revised source code as found ''[[#File Details| File Details]]''.
+
*Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <tt><yoursite>/joomla/administrator/index.php</tt>. For this example we will navigate to <tt>localhost/joomla/administrator/index.php</tt>.</translate>
 +
<translate><!--T:35-->
 +
*Click Menu {{rarr}} Manage {{rarr}} Menu Items {{rarr}} New {{rarr}} Select</translate>
 +
<translate><!--T:36-->
 +
*Navigate and Choose <tt>Hello World!</tt></translate>
 +
<translate><!--T:37-->
 +
*Navigate and Choose</translate> <tt>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLLE</tt>
 +
<translate><!--T:38-->
 +
*Click Menu Title and enter <tt>Hello World!</tt></translate>
 +
<translate><!--T:39-->
 +
*Click Save & Close</translate>
  
Code Explanation:
+
<translate>
<source lang="xml">
+
== File Details == <!--T:40-->
<version>0.0.3</version>
+
</translate>
</source>
+
{{vanchor|site/views/helloworld/tmpl/default.xml}}
 
 
Updates the version number
 
 
 
== File Details ==
 
{{vanchor|site/views/helloworld/tml/default.xml}}
 
 
<source lang="xml>
 
<source lang="xml>
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
Line 116: Line 135:
 
</source>
 
</source>
  
==Component Contents==
+
<translate>
At this point in the tutorial, your component should contain the following files:
+
== Code Explanation == <!--T:41-->
 +
</translate>
 +
<translate><!--T:42-->
 +
In case you were curious as to why this works the way it does.</translate>
 +
 
 +
=== default.xml ===
 +
 
 +
<translate><!--T:43-->
 +
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.</translate>
 +
 
 +
=== helloworld.xml===
 +
 
 +
<source lang="xml">
 +
<version>0.0.3</version>
 +
</source>
 +
 
 +
<translate><!--T:44-->
 +
Updates the version number.</translate>
 +
 
 +
<translate>
 +
== Component Contents == <!--T:45-->
 +
</translate>
 +
<translate><!--T:46-->
 +
At this point in the tutorial, your component should contain the following files:</translate>
 
{| border=1
 
{| border=1
 
  | 1
 
  | 1
 
  | ''[[#helloworld.xml|helloworld.xml]]''
 
  | ''[[#helloworld.xml|helloworld.xml]]''
  | this is an XML (manifest) file that tells Joomla! how to install our component.
+
  | <translate><!--T:47-->
 +
this is an XML (manifest) file that tells Joomla! how to install our component.</translate>
 
  |-
 
  |-
 
  | 2
 
  | 2
 
  | ''[[#site/helloworld.php|site/helloworld.php]]''
 
  | ''[[#site/helloworld.php|site/helloworld.php]]''
  | this is the site entry point to the Hello World! component
+
  | <translate><!--T:48-->
 +
this is the site entry point to the Hello World! component</translate>
 
  |-
 
  |-
 
  | 3
 
  | 3
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:49-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 4
 
  | 4
 
  | ''[[#site/controller.php|site/controller.php]]''
 
  | ''[[#site/controller.php|site/controller.php]]''
  | file representing the controller
+
  | <translate><!--T:50-->
 +
file representing the controller</translate>
 
  |-
 
  |-
 
  | 5
 
  | 5
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:51-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 6
 
  | 6
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:52-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 7
 
  | 7
 
  | ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 
  | ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
  | file representing the view
+
  | <translate><!--T:53-->
 +
file representing the view</translate>
 
  |-
 
  |-
 
  | 8
 
  | 8
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:54-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 9
 
  | 9
 
  | ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''  
 
  | ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''  
  | the default view
+
  | <translate><!--T:55-->
 +
the default view</translate>
 
  |-
 
  |-
 
  | 10
 
  | 10
 
  | ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 
  | ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
  | file that adds menu item
+
  | <translate><!--T:56-->
 +
file that adds menu item</translate>
 
  |-
 
  |-
 
  | 11
 
  | 11
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:57-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 12
 
  | 12
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
  | this is the administrator entry point to the Hello World! component
+
  | <translate><!--T:58-->
 +
this is the administrator entry point to the Hello World! component</translate>
 
  |-
 
  |-
 
  | 13
 
  | 13
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:59-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 14
 
  | 14
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:60-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 15
 
  | 15
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
  | prevents web server from listing directory content
+
  | <translate><!--T:61-->
 +
prevents web server from listing directory content</translate>
 
  |-
 
  |-
 
  | 16
 
  | 16
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 
  | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
  | file allowing to initialise schema version of the com_helloworld component.
+
  | <translate><!--T:62-->
|}
+
file allowing to initialise schema version of the com_helloworld component.</translate>
 
+
|}
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.</translate>
 
 
 
== Contributors ==
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:Jossnaz|Lukas Meier]]
 
*[[User:betweenbrain|Matt Thomas]]
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Kevinkabatra|Kevin Kabatra]]
 
  
 
<div class="row">  
 
<div class="row">  
Line 200: Line 248:
 
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_model_to_the_site_part|Next: Adding a model to the site part</translate>|class=expand}}</div>
 
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_model_to_the_site_part|Next: Adding a model to the site part</translate>|class=expand}}</div>
 
</div>
 
</div>
 
+
__NOTOC__
 
<noinclude>
 
<noinclude>
 +
<translate>
 +
<!--T:63-->
 
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]
Line 212: Line 262:
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials in a Series]]
 
[[Category:Tutorials in a Series]]
 +
</translate>
 
</noinclude>
 
</noinclude>

Revision as of 20:16, 23 January 2016

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 menu item 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 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 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

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 Create: default.xml <path_to_com_helloworld/site/views/helloworld/tmpl/default.xml
2 Update: helloworld.xml <path_to_com_helloworld/helloworld.xml

Updating the Hello World! Component[edit]

To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.

With the component successfully updated, we need to add the Menu Item. This will be done by 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.
  • Click Menu    Manage    Menu Items    New    Select
  • Navigate and Choose Hello World!
  • Navigate and Choose COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLLE
  • Click Menu Title and enter Hello World!
  • Click Save & Close

File Details[edit]

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

Code Explanation[edit]

In case you were curious as to why this works the way it does.

default.xml[edit]

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[edit]

<version>0.0.3</version>

Updates the version number.

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.