Archived

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

From Joomla! Documentation

< Archived:Developing a MVC Component
 
(103 intermediate revisions by 18 users not shown)
Line 1: Line 1:
{{underconstruction}}
+
{{version/tutor|2.5}}
== Indroduction ==
+
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Contents}}
This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!1.6]] tutorial. You are encouraged to read the precedent parts of the tutorial before reading this one.
 
  
In the Joomla!1.6 framework, third party components authors divide their code in three main parts:
+
== Introduction ==
* ''models'' They are responsible for managing the data  
+
This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
* ''controllers'' They perform tasks, set and get states of the models and ask the views to display
+
 
 +
In the Joomla!2.5 framework, third party component authors divide their code into three main parts:
 +
* ''models'' They manage the data  
 +
* ''controllers'' They perform tasks, set and get the states of the models and ask the views to display
 
* ''views'' They display the content according to the type (''error'', ''feed'', ''html'', ''json'', ''raw'', ''xml'') and the layout chosen by the controllers
 
* ''views'' They display the content according to the type (''error'', ''feed'', ''html'', ''json'', ''raw'', ''xml'') and the layout chosen by the controllers
  
 
== Setting the controller ==
 
== Setting the controller ==
In the core code of Joomla, there is a class able to manage controllers: ''JController''. This class has to be extended to be used it in our component. In the file ''site/helloworld.php'' (entry point of our ''Hello World'' component), put these lines
+
In the core code of Joomla, there is a class able to manage controllers: ''[http://api.joomla.org/Joomla-Platform/Application/JController.html JController]''. 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
  
 +
<span id="site/helloworld.php">
 +
''site/helloworld.php''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
// 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
 
// import joomla controller library
 
jimport('joomla.application.component.controller');
 
jimport('joomla.application.component.controller');
 +
 
// Get an instance of the controller prefixed by HelloWorld
 
// Get an instance of the controller prefixed by HelloWorld
 
$controller = JController::getInstance('HelloWorld');
 
$controller = JController::getInstance('HelloWorld');
 +
 
// Perform the Request task
 
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
+
$input = JFactory::getApplication()->input;
 +
$controller->execute($input->getCmd('task'));
 +
 
 
// Redirect if set by the controller
 
// Redirect if set by the controller
$controller->redirect();
+
$controller->redirect();</source>
</source>
+
</span>
The ''getInstance'' static method of the ''JController'' class will create a controller. In the code above, it will create a controller named ''HelloWorldController'' using the ''controller.php'' file
+
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
  
With your favorite files manager and editor, create a ''site/controller.php'' file containing
+
<span id="site/controller.php">
 +
''site/controller.php''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 
// 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
 
// import Joomla controller library
 
jimport('joomla.application.component.controller');
 
jimport('joomla.application.component.controller');
// inherit the JController class
+
 
 +
/**
 +
* Hello World Component Controller
 +
*/
 
class HelloWorldController extends JController
 
class HelloWorldController extends JController
 
{
 
{
 
}
 
}
 
</source>
 
</source>
 +
</span>
 +
 +
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''.
 +
 +
So with ''task'' simply a public function display() of ''JController'' is refered to.
 +
 +
 +
{{tip|Just a side note for completion, you could call another function aside from ''display()'' by using an URL like this one:
 +
 +
<pre>http://localhost/index.php?option=com_helloworld&task=insert</pre>
  
When no task is given in the variables request, 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''.
+
This would try to call a function ''insert()'' of our controller (which we would actually have to implement in ''HelloWorldController'' ).}}
  
 
== Setting the view ==
 
== Setting the view ==
With your favorite files manager and editor, create a file ''site/views/helloworld/view.html.php'' able to display the default view and containing
 
  
 +
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
 
// 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
 
// import Joomla view library
 
jimport('joomla.application.component.view');
 
jimport('joomla.application.component.view');
class HelloWorldViewHelloWorld extends JView {
+
 
protected $msg=null;
+
/**
 +
* HTML View class for the HelloWorld Component
 +
*/
 +
class HelloWorldViewHelloWorld extends JView
 +
{
 
// Overwriting JView display method
 
// Overwriting JView display method
function display($tpl = null) {
+
function display($tpl = null)  
// Assigning data to the view
+
{
 +
// Assign data to the view
 
$this->msg = 'Hello World';
 
$this->msg = 'Hello World';
 +
 +
// Display the view
 
parent::display($tpl);
 
parent::display($tpl);
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 +
</span>
  
The ''display'' method of the ''Jview'' class is called by the ''display'' task of the JController class. In our case, this method will display data using the ''tmpl/default.php'' file. With your favorite files manager and editor, create a file ''site/views/helloworld/tmpl/default.php'' able to display the default view and containing
+
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">
 +
''site/views/helloworld/tmpl/default.php''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 71: Line 119:
 
</source>
 
</source>
  
You can test this basic component by putting ''index.php?option=com_helloworld'' in your browser address (don't forget to prefix this address by your Joomla!1.6 installation path).
+
This template file will be included by the JView class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.
 +
 
 +
 
 +
 
 +
 
 +
So to give an example, one could call the view inside the ''component/com_[component_name]/views/[name of view]/''    folder by calling:
 +
 
 +
http://localhost/joomla/index.php?option=com_helloworld
 +
 
 +
(this would default to the ''component/com_helloworld/views/helloworld/'' folder)
 +
or we could explicitly call the folder by calling
 +
 
 +
http://localhost/joomla/index.php?option=com_helloworld&view=helloworld
 +
 
 +
if we change ''&view=helloworld'' to something else, e.g. ''&view=fluffy'' we would have to create a folder:
 +
 
 +
''component/com_helloworld/views/fluffy/''
 +
 
 +
Copy the contents of ''views/helloworld'' to ''views/fluffy''
 +
 
 +
The classname of the file ''view.html.php'' of the fluffy folder would be ''HelloWorldViewFluffy''. Afterwards you can customize the contents of ''default.php'' of the ''fluffy'' subfolder for custom output and see the output by calling:
 +
 
 +
''http://localhost/joomla/index.php?option=com_helloworld&view=fluffy''
  
 
== Packaging the component ==
 
== Packaging the component ==
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11373/45535/com_helloworld-1.6-part1.zip archive] and install it using the extension manager of Joomla!1.6. You can test this basic component by putting ''index.php?option=com_helloworld'' or ''administrator/index.php?option=com_helloworld'' in your browser address. You can also noticed that the ''Hello World!'' component is visible in the administrator site of your Joomla!1.6 installation under the ''Components'' menu.
+
 
 +
Content of your code directory
 +
* ''[[#helloworld.xml|helloworld.xml]]''
 +
* ''[[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">
 +
''helloworld.xml''
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<extension type="component" version="2.5.0" method="upgrade">
 +
 
 +
<name>Hello World!</name>
 +
<!-- The following elements are optional and free of formatting constraints -->
 +
<creationDate>November 2009</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 in 2.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>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>
 +
</source>
 +
</span>
 +
 
 +
'''Result:'''
 +
You will see by default the message contained in the variable ''$this->msg'' in the ''view.html.php'' file.
 +
 
 +
== Navigate ==
 +
 
 +
[[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]]
  
 
== Contributors ==
 
== Contributors ==
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:cdemko|Christophe Demko]]
 +
*[[User:oaksu|Ozgur Aksu]]
 +
*[[User:Jossnaz|Lukas Meier]]
  
 
[[Category:Development]]
 
[[Category:Development]]
[[category:Joomla! 1.6]]
+
[[Category:Joomla! 1.6]]
[[category:Manual]]
+
[[Category:Joomla! 1.7]]
 +
[[Category:Joomla! 2.5]]

Latest revision as of 17:18, 26 April 2022

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


Introduction[edit]

This tutorial is part of the Developing a Model-View-Controller (MVC) Component for Joomla!2.5 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

In the Joomla!2.5 framework, third party component authors divide their code into three main parts:

  • models They manage the data
  • controllers They perform tasks, set and get the states of the models and ask the views to display
  • views They display the content according to the type (error, feed, html, json, raw, xml) and the layout chosen by the controllers

Setting the controller[edit]

In the core code of Joomla, there is a class able to manage controllers: JController. 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

site/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import joomla controller library
jimport('joomla.application.component.controller');

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

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

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

The 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

site/controller.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
 * Hello World Component Controller
 */
class HelloWorldController extends JController
{
}

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.

So with task simply a public function display() of JController is refered to.


A Tip!

Just a side note for completion, you could call another function aside from display() by using an URL like this one:

http://localhost/index.php?option=com_helloworld&task=insert
This would try to call a function insert() of our controller (which we would actually have to implement in HelloWorldController ).

Setting the view[edit]

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

site/views/helloworld/view.html.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 */
class HelloWorldViewHelloWorld extends JView
{
	// Overwriting JView display method
	function display($tpl = null) 
	{
		// Assign data to the view
		$this->msg = 'Hello World';

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

The display method of the 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

site/views/helloworld/tmpl/default.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>

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



So to give an example, one could call the view inside the component/com_[component_name]/views/[name of view]/    folder by calling:

http://localhost/joomla/index.php?option=com_helloworld

(this would default to the component/com_helloworld/views/helloworld/ folder) or we could explicitly call the folder by calling

http://localhost/joomla/index.php?option=com_helloworld&view=helloworld

if we change &view=helloworld to something else, e.g. &view=fluffy we would have to create a folder:

component/com_helloworld/views/fluffy/

Copy the contents of views/helloworld to views/fluffy

The classname of the file view.html.php of the fluffy folder would be HelloWorldViewFluffy. Afterwards you can customize the contents of default.php of the fluffy subfolder for custom output and see the output by calling:

http://localhost/joomla/index.php?option=com_helloworld&view=fluffy

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 test this basic component by putting index.php?option=com_helloworld in your browser address.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>November 2009</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 in 2.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>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>

Result: You will see by default the message contained in the variable $this->msg in the view.html.php file.

Navigate[edit]

Prev: Developing a Basic Component Next: Adding a menu type to the site part

Contributors[edit]