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
(Changing the place for the TOC)
m
 
(17 intermediate revisions by 9 users not shown)
Line 13: Line 13:
 
* 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]
 
* 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]
 
</translate>
 
</translate>
 +
 +
<translate>
 +
<!--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>
 +
 +
<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>
 +
 +
{{#widget:YouTube|id=HNYEHoAznJE}}
  
 
<translate>
 
<translate>
Line 69: Line 78:
 
  | <translate><!--T:52-->
 
  | <translate><!--T:52-->
 
Create:</translate> [[#site/views/helloworld/tml/default.php| default.php]]
 
Create:</translate> [[#site/views/helloworld/tml/default.php| default.php]]
  | <tt><path_to_joomla>/htdocs/components/com_helloworld/views/helloworld/tmpl/default.php</tt>
+
  | <tt><path_to_com_helloworld>/site/views/helloworld/tmpl/default.php</tt>
 
  |-
 
  |-
 
  | 7
 
  | 7
Line 89: Line 98:
  
 
<translate>
 
<translate>
 +
 
==File Details== <!--T:57-->
 
==File Details== <!--T:57-->
 
</translate>
 
</translate>
{{vanchor|site/helloworld.php}}
+
{{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">
 
<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
Line 98: Line 108:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 123: Line 133:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 145: Line 155:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 185: Line 195:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 196: Line 206:
  
 
{{vanchor|index.html}}
 
{{vanchor|index.html}}
Note - the same code is used for all folders
+
<translate><!--T:88-->
 +
Note - the same code is used for all folders</translate>
 
<source lang="html4strict">
 
<source lang="html4strict">
 
<html><body bgcolor="#FFFFFF"></body></html>
 
<html><body bgcolor="#FFFFFF"></body></html>
Line 204: Line 215:
 
<source lang="xml" highlight="13,30,31">
 
<source lang="xml" highlight="13,30,31">
 
<?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>December 2013</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 256: Line 267:
  
 
<translate>
 
<translate>
 +
 
== Code Explanation == <!--T:58-->
 
== Code Explanation == <!--T:58-->
 
</translate>
 
</translate>
Line 292: Line 304:
  
 
<translate><!--T:64-->
 
<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.</translate>
+
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 ===
 
=== controller.php ===
Line 299: Line 311:
 
class HelloWorldController extends JControllerLegacy
 
class HelloWorldController extends JControllerLegacy
 
{
 
{
function display($tpl = null)
 
{
 
// Assign data to the view
 
$this->msg = 'Hello World';
 
 
// Display the view
 
parent::display($tpl);
 
}
 
 
}
 
}
 
</source>
 
</source>
Line 317: Line 321:
 
class HelloWorldViewHelloWorld extends JViewLegacy
 
class HelloWorldViewHelloWorld extends JViewLegacy
 
{
 
{
 +
function display($tpl = null)
 +
{
 +
// Assign data to the view
 +
$this->msg = 'Hello World';
 +
 +
// Display the view
 +
parent::display($tpl);
 +
}
 +
}
 
</source>
 
</source>
  
 
<translate><!--T:66-->
 
<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>
 
''[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 ===
 
=== default.php ===

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.