J3.x

Difference between revisions of "Developing an MVC Component/Adding a variable request in the menu type"

From Joomla! Documentation

< J3.x:Developing an MVC Component
 
(20 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{:J3.1:Developing a MVC Component}}
+
<noinclude><languages /></noinclude>
 +
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 +
en</translate>}}
 +
<translate>== Introduction == <!--T:2--></translate>
 +
<translate><!--T:3-->
 +
This tutorial is part of the [[S:MyLanguage/J3.2:Developing an MVC Component | Developing a MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
 +
<translate>
 +
<!--T:19-->
 +
You can watch a video associated with this step in the tutorial at [https://www.youtube.com/watch?v=nqy_vhiS4so Step 5, adding a variable request in the menu].</translate>
  
== Introduction ==
+
{{#widget:YouTube|id=nqy_vhiS4so}}
This tutorial is part of the [[J3.2:Developing a MVC Component | Developing a MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
 
  
== Adding a variable request in the menu type ==
+
<translate>== Adding a variable request in the menu type == <!--T:4-->
For the moment, the displayed message is always ''Hello World!''. Joomla!2.5 gives the possibility to add parameters to menu types. In our case, this is done in the ''site/views/helloworld/tmpl/default.xml'' file:
+
For the moment, the displayed message is always ''Hello World!''. Since Joomla! 2.5, it is possible to add parameters to menu types. In our case, this is done in the <tt>site/views/helloworld/tmpl/default.xml</tt> file:</translate>
  
 
<span id="site/views/helloworld/tmpl/default.xml">
 
<span id="site/views/helloworld/tmpl/default.xml">
''site/views/helloworld/tmpl/default.xml''
+
'''''site/views/helloworld/tmpl/default.xml'''''
<source lang="xml">
+
<source lang="xml" highlight="6-18">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
 
<metadata>
 
<metadata>
Line 18: Line 25:
 
<fieldset name="request">
 
<fieldset name="request">
 
<field
 
<field
name="id"
+
name="id"
type="list"
+
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
+
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
+
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
default="1">
+
default="1">
<option value="1">Hello World!</option>
+
                                <option value="1">Hello World!</option>
<option value="2">Good bye World!</option>
+
                                <option value="2">Good bye World!</option>
</field>
+
                        </field>
 
</fieldset>
 
</fieldset>
 
</fields>
 
</fields>
Line 32: Line 39:
 
</span>
 
</span>
  
Two important things to note:
+
<translate>
* the ''request'' group of fields indicates mandatory fields
+
<!--T:18-->
* the name attribute can be passed to the component in the URL. In this case ''?option=com_helloworld&id=1''  would indicate that you had chosen option 1.
+
We're including a type ''list'' field within the menu. This is one of the Joomla [https://docs.joomla.org/Standard_form_field_types Standard Form Field Types].</translate>
  
The model has to be modified in order to switch between the two different messages (which is chosen by the user with the field defined above):
+
<translate><!--T:5-->
 +
Two important things to note:</translate>
 +
<translate><!--T:6-->
 +
* the ''request'' group of fields indicates mandatory fields.</translate>
 +
<translate><!--T:7-->
 +
* the name attribute can be passed to the component in the URL. In this case <tt>?option=com_helloworld&id=1</tt> would indicate that you had chosen option 1.</translate>
 +
 
 +
<translate><!--T:8-->
 +
The model has to be modified in order to switch between the two different messages (depending upon which is chosen by the administrator  with the field defined above):</translate>
  
 
<span id="site/models/helloworld.php">
 
<span id="site/models/helloworld.php">
''site/models/helloworld.php''
+
'''''site/models/helloworld.php'''''
<source lang="php">
+
<source lang="php" highlight="33-45">
 
<?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
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
 
jimport('joomla.application.component.modelitem');
 
  
 
/**
 
/**
 
  * HelloWorld Model
 
  * HelloWorld Model
 +
*
 +
* @since  0.0.1
 
  */
 
  */
 
class HelloWorldModelHelloWorld extends JModelItem
 
class HelloWorldModelHelloWorld extends JModelItem
 
{
 
{
 
/**
 
/**
* @var string msg
+
* @var string message
 
*/
 
*/
protected $msg;
+
protected $message;
+
 
 
/**
 
/**
 
* Get the message
 
* Get the message
* @return string The message to be displayed to the user
+
* @return string The message to be displayed to the user
 
*/
 
*/
public function getMsg()  
+
public function getMsg()
 
{
 
{
if (!isset($this->msg))  
+
if (!isset($this->message))
 
{
 
{
 
 
$jinput = JFactory::getApplication()->input;
 
$jinput = JFactory::getApplication()->input;
 
$id    = $jinput->get('id', 1, 'INT');
 
$id    = $jinput->get('id', 1, 'INT');
  
switch ($id)  
+
switch ($id)
 
{
 
{
case 2:
+
case 2:
$this->msg = 'Good bye World!';
+
$this->message = 'Good bye World!';
break;
+
break;
default:
+
default:
case 1:
+
case 1:
$this->msg = 'Hello World!';
+
$this->message = 'Hello World!';
break;
+
break;
 
}
 
}
 
}
 
}
return $this->msg;
+
return $this->message;
 
}
 
}
 
}
 
}
Line 87: Line 108:
 
</span>
 
</span>
  
Also modify your ''helloworld.xml'' file to indicate the new version:
+
<translate><!--T:9-->
 +
Also modify your <tt>helloworld.xml</tt> file to indicate the new version:</translate>
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
''helloworld.xml''
+
<tt>helloworld.xml</tt>
<source lang="xml">
+
<source lang="xml" highlight="13">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">
+
<extension type="component" version="3.0" method="upgrade">
  
 
<name>Hello World!</name>
 
<name>Hello World!</name>
 
<!-- The following elements are optional and free of formatting constraints -->
 
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2014</creationDate>
+
<creationDate>January 2018</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
 
<authorEmail>john.doe@example.org</authorEmail>
 
<authorEmail>john.doe@example.org</authorEmail>
Line 108: Line 130:
 
<description>Description of the Hello World component ...</description>
 
<description>Description of the Hello World component ...</description>
  
<update> <!-- Runs on update; New in 2.5 -->
+
<update> <!-- Runs on update; New since J2.5 -->
 
<schemas>
 
<schemas>
 
<schemapath type="mysql">sql/updates/mysql</schemapath>
 
<schemapath type="mysql">sql/updates/mysql</schemapath>
Line 128: Line 150:
 
<administration>
 
<administration>
 
<!-- Administration Menu Section -->
 
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
+
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
 
<!-- Administration Main File Copy Section -->
 
<!-- Administration Main File Copy Section -->
 
<!-- Note the folder attribute: This attribute describes the folder
 
<!-- Note the folder attribute: This attribute describes the folder
Line 146: Line 168:
 
</span>
 
</span>
  
You can test this variable request by putting ''index.php?option=com_helloworld&id=1'' or ''index.php?option=com_helloworld&id=2'' in your browser address.
+
<translate><!--T:10-->
 +
You can test this variable request by putting <tt>index.php?option=com_helloworld&view=helloworld&id=1</tt> or <tt>index.php?option=com_helloworld&view=helloworld&id=2</tt> in your browser address.</translate>
  
== Packaging the component ==
+
<translate>== Packaging the component == <!--T:11-->
 +
Content of your code directory:</translate>
  
Content of your code directory
 
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
* ''[[J3.2:Developing_a_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]]''
 
* ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 
* ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
 
* ''[[#site/models/helloworld.php|site/models/helloworld.php]]''
 
* ''[[#site/models/helloworld.php|site/models/helloworld.php]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
* ''[[J3.2:Developing_a_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]]''
 
 
Create a compressed file of this directory or directly download the [https://github.com/joomla/Joomla-3.2-Hello-World-Component/archive/step-5-adding-a-menu-variable-request.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
 
  
{{notice|Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.}}
+
<translate><!--T:12-->
 +
Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-5-adding-a-menu-variable-request.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.</translate>
  
{{:J3.2:Developing a MVC Component/Navigate
+
{{notice|<translate><!--T:13-->
|prev=Adding a model to the site part <!-- previous article subpage name -->
+
Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.</translate>}}
|next=Using the database <!-- next article subpage name -->}}
 
  
== Contributors ==
+
<translate>== Contributors == <!--T:14--></translate>
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:betweenbrain|Matt Thomas]]
 
*[[User:betweenbrain|Matt Thomas]]
 +
*[[User:scionescire|Scionescire]]
  
 +
<div class="row">
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:15-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding a model to the site part|Prev: Adding a model to the site part</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:16-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Using the database|Next: Using the database</translate>|class=expand}}</div>
 +
</div>
 +
__NOTOC__
 +
<noinclude>
 +
<translate>
 +
<!--T:17-->
 +
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.2]]
 
[[Category:Joomla! 3.2]]
 +
[[Category:Joomla! 3.3]]
 +
[[Category:Joomla! 3.4]]
 
[[Category:Beginner Development]]
 
[[Category:Beginner Development]]
 
[[Category:Component Development]]
 
[[Category:Component Development]]
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 +
</translate>
 +
</noinclude>

Latest revision as of 17:18, 25 January 2018

Other languages:
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).



Introduction[edit]

This tutorial is part of the Developing a MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this. You can watch a video associated with this step in the tutorial at Step 5, adding a variable request in the menu.

Adding a variable request in the menu type[edit]

For the moment, the displayed message is always Hello World!. Since Joomla! 2.5, it is possible to add parameters to menu types. In our case, this is done in the site/views/helloworld/tmpl/default.xml file:

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>
	<fields name="request">
		<fieldset name="request">
			<field
					name="id"
					type="list"
					label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
					description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
					default="1">
                                <option value="1">Hello World!</option>
                                <option value="2">Good bye World!</option>
                        </field>
		</fieldset>
	</fields>
</metadata>

We're including a type list field within the menu. This is one of the Joomla Standard Form Field Types.

Two important things to note:

  • the request group of fields indicates mandatory fields.
  • the name attribute can be passed to the component in the URL. In this case ?option=com_helloworld&id=1 would indicate that you had chosen option 1.

The model has to be modified in order to switch between the two different messages (depending upon which is chosen by the administrator with the field defined above):

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))
		{
			$jinput = JFactory::getApplication()->input;
			$id     = $jinput->get('id', 1, 'INT');

			switch ($id)
			{
				case 2:
					$this->message = 'Good bye World!';
					break;
				default:
				case 1:
					$this->message = 'Hello World!';
					break;
			}
		}
		return $this->message;
	}
}

Also modify your helloworld.xml file to indicate the new version:

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

You can test this variable request by putting index.php?option=com_helloworld&view=helloworld&id=1 or index.php?option=com_helloworld&view=helloworld&id=2 in your browser address.

Packaging the component[edit]

Content of your code directory:

Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.

Info non-talk.png
General Information

Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.

Contributors[edit]