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
m (Fix the intro inclusion)
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{:J3.1:Developing a MVC Component}}
+
{{:J3.x:Developing a MVC Component/en}}
 
 
{{review}}
 
 
 
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
 
  
 
== Introduction ==
 
== Introduction ==
Line 12: Line 8:
  
 
<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 22: Line 18:
 
<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 44: Line 39:
  
 
<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 - 2015 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 96: Line 97:
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
 
''helloworld.xml''
 
''helloworld.xml''
<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="2.5.0" method="upgrade">
+
<extension type="component" version="3.2.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>November 2009</creationDate>
+
<creationDate>January 2014</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
 
<authorEmail>john.doe@example.org</authorEmail>
 
<authorEmail>john.doe@example.org</authorEmail>
Line 113: Line 114:
 
<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 133: Line 134:
 
<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 157: Line 158:
 
Content of your code directory
 
Content of your code directory
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/helloworld.php|site/helloworld.php]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/controller.php|site/controller.php]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/helloworld/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_04#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
+
* ''[[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]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''
+
* ''[[J3.2:Developing_a_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_-_Part_02#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
+
* ''[[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]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/models/index.html]]''
+
* ''[[J3.2:Developing_a_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_-_Part_01#index.html|admin/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#admin/helloworld.php|admin/helloworld.php]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/updates/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''
+
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
* ''[[J3.2:Developing a MVC Component_-_Part_01#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
+
* ''[[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]]''
 
 
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.
 
  
 +
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.
  
== Navigate ==
+
{{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.}}
  
[[J3.2:Developing a MVC Component - Part 04|Prev: Adding a model to the site part]]
+
{{:J3.2:Developing a MVC Component/Navigate
[[J3.2:Developing a MVC Component - Part 06|Next: Using the database]]
+
|prev=Adding a model to the site part <!-- previous article subpage name -->
 +
|next=Using the database <!-- next article subpage name -->}}
  
 
== Contributors ==
 
== Contributors ==
Line 187: Line 188:
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:betweenbrain|Matt Thomas]]
 
*[[User:betweenbrain|Matt Thomas]]
 +
*[[User:scionescire|Scionescire]]
  
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]

Revision as of 07:05, 27 June 2015

Joomla! 
3.x
Tutorial
Developing a 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.

Adding a variable request in the menu type[edit]

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:

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>

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 (which is chosen by the user with the field defined above):

site/models/helloworld.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 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.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.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&id=1 or index.php?option=com_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.

J3.x:Developing a MVC Component/Navigate

Contributors[edit]