J3.x

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

From Joomla! Documentation

< J3.x:Developing an MVC Component
m
 
(82 intermediate revisions by 24 users not shown)
Line 1: Line 1:
{{version/tutor|3.2}}
+
<noinclude><languages /></noinclude>
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!3.1 - Contents}}
+
{{:J3.1:Developing an MVC Component/<translate><!--T:6-->
 +
en</translate>}}
  
== Introduction ==
+
<translate>
This tutorial is part of the [[J3.2:Developing_a_MVC_Component|Developing a Model-View-Controller (MVC) Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
+
==Note== <!--T:37-->
 +
</translate>
 +
<translate><!--T:38-->
 +
* 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>
  
In the Joomla!3.x framework, third party component authors divide their code into three main parts:
+
<translate>
* ''models'' They manage the data
+
<!--T:39-->
* ''controllers'' They perform tasks, set and get the states of the models and ask the views to display
+
* 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-2-adding-a-site-view.zip archive]
* ''views'' They display the content according to the type (''error'', ''feed'', ''html'', ''json'', ''raw'', ''xml'') and the layout chosen by the controllers
+
</translate>
  
== Setting the controller ==
+
<translate>
In the core code of Joomla, there is a class able to manage controllers: ''[http://api.joomla.org/cms-3/classes/JControllerLegacy.html JControllerLegacy]''. This class has to be extended to be used in our component. In the file ''site/helloworld.php'' (entry point of our ''Hello World'' component), put these lines
+
<!--T:90-->
 +
* As you follow through the steps of the tutorial you may find problems. In this case you may find it useful to read the Joomla page on [[S:MyLanguage/How_to_debug_your_code|How to debug your code]].
 +
</translate>
  
{{vanchor|site/helloworld.php}}
+
<translate><!--T:91--> * You can watch a video associated with this step in the tutorial at [https://www.youtube.com/watch?v=HNYEHoAznJE Step 2, Adding a View].</translate>
<source lang="php">
+
 
 +
{{#widget:YouTube|id=HNYEHoAznJE}}
 +
 
 +
<translate>
 +
==Adding a View to Hello World!== <!--T:40-->
 +
</translate>
 +
__TOC__
 +
<translate><!--T:41-->
 +
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 [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.</translate>
 +
 
 +
<translate><!--T:42-->
 +
There are several ways to update a Joomla! component. In this tutorial we will focus option 2.</translate>
 +
{| border=1
 +
| 1
 +
| <translate><!--T:43-->
 +
Manually add the files into <tt><path_to_joomla>/</tt></translate>
 +
|-
 +
| 2
 +
| <translate><!--T:44-->
 +
Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install</translate>
 +
|-
 +
|3
 +
| <translate><!--T:45-->
 +
Update using the Joomla! Extension Manager and an [[S:MyLanguage/Deploying_an_Update_Server| Update Server]]</translate>
 +
|}
 +
 
 +
<translate><!--T:46-->
 +
To add a view you will need to navigate to <tt>com_helloworld</tt>, which is the original directory we made for our component. 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
 +
| 1
 +
| <translate><!--T:47-->
 +
Update:</translate> [[#site/helloworld.php| helloworld.php]]
 +
| <tt><path_to_com_helloworld>/site/helloworld.php</tt>
 +
|-
 +
| 2
 +
| <translate><!--T:48-->
 +
Create:</translate> [[#site/controller.php| controller.php]]
 +
| <tt><path_to_com_helloworld>/site/controller.php</tt>
 +
|-
 +
| 3
 +
| <translate><!--T:49-->
 +
Create:</translate> [[#index.html| index.html]]
 +
| <tt><path_to_com_helloworld>/site/views/index.html</tt>
 +
|-
 +
| 4
 +
| <translate><!--T:50-->
 +
Create:</translate> [[#index.html| index.html]]
 +
| <tt><path_to_com_helloworld>/site/views/helloworld/index.html</tt>
 +
|-
 +
| 5
 +
| <translate><!--T:51-->
 +
Create:</translate> [[#site/views/helloworld/view.html.php| view.html.php]]
 +
| <tt><path_to_com_helloworld>/site/views/helloworld/view.html.php</tt>
 +
|-
 +
| 6
 +
| <translate><!--T:52-->
 +
Create:</translate> [[#site/views/helloworld/tml/default.php| default.php]]
 +
| <tt><path_to_com_helloworld>/site/views/helloworld/tmpl/default.php</tt>
 +
|-
 +
| 7
 +
| <translate><!--T:53-->
 +
Create:</translate> [[#index.html| index.html]]
 +
| <tt><path_to_com_helloworld>/site/views/helloworld/tmpl/index.html</tt>
 +
|-
 +
| 8
 +
| <translate><!--T:54-->
 +
Update:</translate> [[#helloworld.xml| helloworld.xml]]
 +
| <tt><path_to_com_helloworld/helloworld.xml</tt>
 +
|}
 +
 
 +
<translate>
 +
=== Updating the Hello World! Component === <!--T:55-->
 +
</translate>
 +
<translate><!--T:56-->
 +
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>
 +
 
 +
<translate>
 +
 
 +
==File Details== <!--T:57-->
 +
</translate>
 +
{{vanchor|site/helloworld.php}} (Note that some browsers may not copy and paste highlighted text correctly into a text editor. If you find this, then please try a different browser).
 +
<source lang="php" highlight="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21">
 
<?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 controller library
 
jimport('joomla.application.component.controller');
 
  
 
// Get an instance of the controller prefixed by HelloWorld
 
// Get an instance of the controller prefixed by HelloWorld
Line 32: Line 125:
 
$controller->redirect();
 
$controller->redirect();
 
</source>
 
</source>
 
The ''[http://api.joomla.org/Joomla-Platform/Application/JController.html#getInstance getInstance]'' static method of the ''JController'' class will create a controller. In the code above, it will instantiate a controller object of a class named ''HelloWorldController''. Joomla will look for the declaration of that class in an aptly named file called ''controller.php'' (it's a default behavior).
 
 
Now, ''controller.php'' needs to be created and ''HelloWorldController'' needs to be declared and defined. So with your favorite file manager and editor, create a ''site/controller.php'' file containing
 
  
 
{{vanchor|site/controller.php}}
 
{{vanchor|site/controller.php}}
 
<source lang="php">
 
<source lang="php">
 
<?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 controller library
 
jimport('joomla.application.component.controller');
 
 
 
/**
 
/**
 
  * Hello World Component Controller
 
  * Hello World Component Controller
 +
*
 +
* @since  0.0.1
 
  */
 
  */
 
class HelloWorldController extends JControllerLegacy
 
class HelloWorldController extends JControllerLegacy
Line 54: Line 148:
 
</source>
 
</source>
  
When no task is given in the request variables, the default task will be executed. It's the ''display'' task by default. The ''JController'' class has such a task. In our example, it will display a view named ''HelloWorld''.
+
{{vanchor|site/views/helloworld/view.html.php}}
 
 
== Setting the view ==
 
 
 
When JController wants to display a view, it will look for certain files in the ''component/com_[component_name]/views/[name of view]/'' folder.
 
 
 
The name of the folder of the default view is the name of the component itself. In our case the path is ''component/com_helloworld/views/helloworld/''.
 
 
 
The file that will contain the code of the view is called ''view.[view_mode].php''. The default view mode, and probably the only view a component might need is the ''html'' mode. So this give us our file name which is ''view.html.php''.
 
 
 
With your favorite file manager and editor, create a file ''site/views/helloworld/view.html.php'' able to display the default view and containing
 
 
 
<span id="site/views/helloworld/view.html.php">
 
''site/views/helloworld/view.html.php''
 
 
<source lang="php">
 
<source lang="php">
 
<?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 view library
 
jimport('joomla.application.component.view');
 
  
 
/**
 
/**
 
  * HTML View class for the HelloWorld Component
 
  * HTML View class for the HelloWorld Component
 +
*
 +
* @since  0.0.1
 
  */
 
  */
 
class HelloWorldViewHelloWorld extends JViewLegacy
 
class HelloWorldViewHelloWorld extends JViewLegacy
 
{
 
{
// Overwriting JView display method
+
/**
function display($tpl = null)  
+
* Display the Hello World view
 +
*
 +
* @param  string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 +
*
 +
* @return  void
 +
*/
 +
function display($tpl = null)
 
{
 
{
 
// Assign data to the view
 
// Assign data to the view
Line 91: Line 185:
 
}
 
}
 
}
 
}
 +
 
</source>
 
</source>
</span>
 
 
The ''display'' method of the ''[http://api.joomla.org/Joomla-Platform/Application/JView.html JView]'' class is called with the ''display'' task of the JController class. In our case, this method will display data using the ''tmpl/default.php'' file. With your favorite file manager and editor, create a file ''site/views/helloworld/tmpl/default.php'' able to display the default view and containing
 
  
<span id="site/views/helloworld/tmpl/default.php">
+
{{vanchor|site/views/helloworld/tmpl/default.php}}
''site/views/helloworld/tmpl/default.php''
 
 
<source lang="php">
 
<source lang="php">
 
<?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');
Line 106: Line 205:
 
</source>
 
</source>
  
This template file will be included by the JView class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.
+
{{vanchor|index.html}}
 
+
<translate><!--T:88-->
== Packaging the component ==
+
Note - the same code is used for all folders</translate>
 
+
<source lang="html4strict">
Content of your code directory
+
<html><body bgcolor="#FFFFFF"></body></html>
* ''[[#helloworld.xml|helloworld.xml]]''
+
</source>
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/index.html]]''
 
* ''[[#site/helloworld.php|site/helloworld.php]]''
 
* ''[[#site/controller.php|site/controller.php]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/index.html]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/index.html]]''
 
* ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''
 
* ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/index.html]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/index.html]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/index.html]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''
 
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#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 [http://joomlacode.org/gf/download/frsrelease/11394/58226/com_helloworld-1.6-part02.zip archive] and install it using the extension manager of Joomla. You can test this basic component by putting ''index.php?option=com_helloworld'' in your browser address.
 
  
<span id="helloworld.xml">
+
{{vanchor|helloworld.xml}}
''helloworld.xml''
+
<source lang="xml" highlight="13,30,31">
<source lang="xml">
 
 
<?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.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 2018</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
 
<authorEmail>john.doe@example.org</authorEmail>
 
<authorEmail>john.doe@example.org</authorEmail>
Line 148: Line 230:
 
<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 167: Line 249:
 
<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 183: Line 265:
 
</extension>
 
</extension>
 
</source>
 
</source>
</span>
 
  
'''Result:'''
+
<translate>
You will see by default the message contained in the variable ''$this->msg'' in the ''view.html.php'' file.
+
 
 +
== Code Explanation == <!--T:58-->
 +
</translate>
 +
<translate><!--T:59-->
 +
In case you were curious as to why this works the way it does.</translate>
 +
 
 +
=== helloworld.php ===
 +
<source lang="php>
 +
defined('_JEXEC') or die('Restricted access');
 +
</source>
 +
 
 +
<translate><!--T:60-->
 +
This enables for a secure entry point into the Joomla! platform. [[JEXEC| JEXEC]] contains a detailed explanation.</translate>
 +
 
 +
<source lang="php">
 +
$controller = JControllerLegacy::getInstance('HelloWorld');
 +
</source>
 +
 
 +
<translate><!--T:61-->
 +
''[https://api.joomla.org/cms-3/classes/JControllerLegacy.html JControllerLegacy]'' is a base class for a Joomla! Controller. In order for our website to use controllers, we must extend this class in our component. The ''[https://api.joomla.org/cms-3/classes/JControllerLegacy.html#method_getInstance getInstance]'' static method of the ''JControllerLegacy'' class will create a controller. In the code above, it will instantiate a controller object of a class named ''HelloWorldController''. Joomla will look for the declaration of that class in <tt><path_to_joomla>/htdocs/components/com_helloworld/controller.php</tt>.</translate>
 +
 
 +
<source lang="php">
 +
$input = JFactory::getApplication()->input;
 +
$controller->execute($input->getCmd('task'));
 +
</source>
 +
 
 +
<translate><!--T:62-->
 +
After the controller is created, we instruct the controller to execute the task, as defined in the URL: <tt><yoursite>/joomla/index.php?option=com_helloworld&task=<task_name></tt>. If no task is set, the default task 'display' will be assumed. When display is used, the 'view' variable will decide what will be displayed. Other common tasks are save, edit, new, etc.</translate>
 +
 
 +
<source lang="php">
 +
$controller->redirect();
 +
</source>
 +
 
 +
<translate><!--T:63-->
 +
The controller might decide to redirect the page, usually after a task like 'save' has been completed. This last statement takes care of the actual redirection.</translate>
 +
 
 +
<translate><!--T:64-->
 +
The main entry point, helloworld.php, essentially passes control to the controller, which handles performing the task that was specified in the request.  Our component specific controller doesn't do anything more than the parent class already does, which is why our controller class is empty.</translate>
 +
 
 +
=== controller.php ===
 +
 
 +
<source lang="php">
 +
class HelloWorldController extends JControllerLegacy
 +
{
 +
}
 +
</source>
 +
<translate><!--T:65-->
 +
When no task is given in the request variables, the default task will be executed. It's the display task by default. The JControllerLegacy class has such a task. In our example, it will display a view named HelloWorld.</translate>
 +
 
 +
=== view.html.php ===
 +
 
 +
<source lang="php">
 +
class HelloWorldViewHelloWorld extends JViewLegacy
 +
{
 +
function display($tpl = null)
 +
{
 +
// Assign data to the view
 +
$this->msg = 'Hello World';
 +
 +
// Display the view
 +
parent::display($tpl);
 +
}
 +
}
 +
</source>
 +
 
 +
<translate><!--T:66-->
 +
The view sets up the text to be output and then calls the base display class.
 +
''[https://api.joomla.org/cms-3/classes/JViewLegacy.html JViewLegacy]'' is a base class for a Joomla! View. In our case, this method will display data using the tmpl/default.php file.</translate>
 +
 
 +
<translate><!--T:89-->
 +
'''Caveat:''' If your view needs to load or otherwise embed individual JavaScript code, do not do that in the view, as the code might not get included when caching is enabled. Load those scripts in the controller instead. See
 +
[https://issues.joomla.org/tracker/joomla-cms/11203 the related issue on the issue tracker].</translate>
 +
 
 +
=== default.php ===
 +
 
 +
<source lang="php">
 +
<h1><?php echo $this->msg; ?></h1>
 +
</source>
 +
 
 +
<translate><!--T:67-->
 +
This template file will be included by the JViewLegacy class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.</translate>
 +
 
 +
=== helloworld.xml ===
 +
 
 +
<source lang="xml">
 +
<version>0.0.2</version>
 +
</source>
 +
 
 +
<translate><!--T:68-->
 +
Updates the version number.</translate>
 +
 
 +
<source lang="xml">
 +
<filename>controller.php</filename>
 +
<folder>views</folder>
 +
</source>
 +
 
 +
<translate><!--T:69-->
 +
Tells installer application to add controller.php and the views/directory</translate>
  
== Navigate ==
+
<translate>
 +
==Component Contents== <!--T:70-->
 +
</translate>
 +
<translate><!--T:71-->
 +
At this point in the tutorial, your component should contain the following files:</translate>
 +
{| border=1
 +
| 1
 +
| ''[[#helloworld.xml|helloworld.xml]]''
 +
| <translate><!--T:72-->
 +
this is an XML (manifest) file that tells Joomla! how to install our component.</translate>
 +
|-
 +
| 2
 +
| ''[[#site/helloworld.php|site/helloworld.php]]''
 +
| <translate><!--T:73-->
 +
this is the site entry point to the Hello World! component</translate>
 +
|-
 +
| 3
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
 +
| <translate><!--T:74-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 4
 +
| ''[[#site/controller.php|site/controller.php]]''
 +
| <translate><!--T:75-->
 +
file representing the controller</translate>
 +
|-
 +
| 5
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 +
| <translate><!--T:76-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 6
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 +
| <translate><!--T:77-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 7
 +
| ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 +
| <translate><!--T:78-->
 +
file representing the view</translate>
 +
|-
 +
| 8
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 +
| <translate><!--T:79-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 9
 +
| ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 +
| <translate><!--T:80-->
 +
the default view</translate>
 +
|-
 +
| 10
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 +
| <translate><!--T:81-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 11
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
 +
| <translate><!--T:82-->
 +
this is the administrator entry point to the Hello World! component</translate>
 +
|-
 +
| 12
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 +
| <translate><!--T:83-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 13
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 +
| <translate><!--T:84-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 14
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 +
| <translate><!--T:85-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 15
 +
| ''[[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]]''
 +
| <translate><!--T:86-->
 +
file allowing to initialise schema version of the com_helloworld component.</translate>
 +
|}
  
[[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 01|Prev: Developing a Basic Component]] [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 03|Next: Adding a menu type to the site part]]
+
{{notice|<translate><!--T:18-->
 +
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>}}
  
== Contributors ==
+
<div class="row">
*[[User:cdemko|Christophe Demko]]
+
<div class="large-6 columns">{{Basic button|<translate><!--T:3-->
*[[User:oaksu|Ozgur Aksu]]
+
S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component|Prev: Developing a Basic Component</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:4-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_menu_type_to_the_site_part|Next: Adding a menu type to the site part</translate>|class=expand}}</div>
 +
</div>
  
[[Category:Development]]
+
<noinclude>
 +
<translate>
 +
<!--T:87-->
 +
[[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:Component Development]]
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 +
</translate>
 +
</noinclude>

Latest revision as of 22:20, 21 January 2019

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎português do Brasil • ‎русский • ‎العربية • ‎中文(台灣)‎
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
  • As you follow through the steps of the tutorial you may find problems. In this case you may find it useful to read the Joomla page on How to debug your code.

Adding a View 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. In this 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 view you will need to navigate to com_helloworld, which is the original directory we made for our component. 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 Update: helloworld.php <path_to_com_helloworld>/site/helloworld.php
2 Create: controller.php <path_to_com_helloworld>/site/controller.php
3 Create: index.html <path_to_com_helloworld>/site/views/index.html
4 Create: index.html <path_to_com_helloworld>/site/views/helloworld/index.html
5 Create: view.html.php <path_to_com_helloworld>/site/views/helloworld/view.html.php
6 Create: default.php <path_to_com_helloworld>/site/views/helloworld/tmpl/default.php
7 Create: index.html <path_to_com_helloworld>/site/views/helloworld/tmpl/index.html
8 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.


File Details[edit]

site/helloworld.php (Note that some browsers may not copy and paste highlighted text correctly into a text editor. If you find this, then please try a different browser).

<?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');

// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

site/controller.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');
/**
 * Hello World Component Controller
 *
 * @since  0.0.1
 */
class HelloWorldController extends JControllerLegacy
{
}

site/views/helloworld/view.html.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');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld extends JViewLegacy
{
	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		// Assign data to the view
		$this->msg = 'Hello World';

		// Display the view
		parent::display($tpl);
	}
}

site/views/helloworld/tmpl/default.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');
?>
<h1><?php echo $this->msg; ?></h1>

index.html Note - the same code is used for all folders

<html><body bgcolor="#FFFFFF"></body></html>

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

helloworld.php[edit]

defined('_JEXEC') or die('Restricted access');

This enables for a secure entry point into the Joomla! platform. JEXEC contains a detailed explanation.

$controller = JControllerLegacy::getInstance('HelloWorld');

JControllerLegacy is a base class for a Joomla! Controller. In order for our website to use controllers, we must extend this class in our component. The getInstance static method of the JControllerLegacy class will create a controller. In the code above, it will instantiate a controller object of a class named HelloWorldController. Joomla will look for the declaration of that class in <path_to_joomla>/htdocs/components/com_helloworld/controller.php.

$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

After the controller is created, we instruct the controller to execute the task, as defined in the URL: <yoursite>/joomla/index.php?option=com_helloworld&task=<task_name>. If no task is set, the default task 'display' will be assumed. When display is used, the 'view' variable will decide what will be displayed. Other common tasks are save, edit, new, etc.

$controller->redirect();

The controller might decide to redirect the page, usually after a task like 'save' has been completed. This last statement takes care of the actual redirection.

The main entry point, helloworld.php, essentially passes control to the controller, which handles performing the task that was specified in the request. Our component specific controller doesn't do anything more than the parent class already does, which is why our controller class is empty.

controller.php[edit]

class HelloWorldController extends JControllerLegacy
{
}

When no task is given in the request variables, the default task will be executed. It's the display task by default. The JControllerLegacy class has such a task. In our example, it will display a view named HelloWorld.

view.html.php[edit]

class HelloWorldViewHelloWorld extends JViewLegacy
{
	function display($tpl = null)
	{
		// Assign data to the view
		$this->msg = 'Hello World';
 
		// Display the view
		parent::display($tpl);
	}
}

The view sets up the text to be output and then calls the base display class. JViewLegacy is a base class for a Joomla! View. In our case, this method will display data using the tmpl/default.php file.

Caveat: If your view needs to load or otherwise embed individual JavaScript code, do not do that in the view, as the code might not get included when caching is enabled. Load those scripts in the controller instead. See the related issue on the issue tracker.

default.php[edit]

<h1><?php echo $this->msg; ?></h1>

This template file will be included by the JViewLegacy class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.

helloworld.xml[edit]

<version>0.0.2</version>

Updates the version number.

<filename>controller.php</filename>
<folder>views</folder>

Tells installer application to add controller.php and the views/directory

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 admin/index.html prevents web server from listing directory content
11 admin/helloworld.php this is the administrator entry point to the Hello World! component
12 admin/sql/index.html prevents web server from listing directory content
13 admin/sql/updates/index.html prevents web server from listing directory content
14 admin/sql/updates/mysql/index.html prevents web server from listing directory content
15 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.
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.