User

Rvsjoen/tutorial/Developing an MVC Component/Part 02: Difference between revisions

From Joomla! Documentation

Rvsjoen (talk | contribs)
Created page with "This tutorial is for {{JVer|1.6}} == Articles in this series == {{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!1.6 - Contents}} == Introduction == This t..."
 
Rvsjoen (talk | contribs)
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This tutorial is for {{JVer|1.6}}
= Adding a view to the frontend =


== Articles in this series ==
In order to create a basic view for the frontend, we first need to have some logic in our previously created entry point <tt>site/helloworld.php</tt> that decides how to handle requests which are passed to the component. We will manage this by having the entry point load up a controller and execute it, so the first order of business is telling the entry point what to do, and then creating a controller.
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!1.6 - Contents}}
 
== Introduction ==
This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!1.6]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.


In the Joomla!1.6 framework, third party component authors divide their code into three main parts:
== Creating a controller ==
* ''models'' They manage the data
In the core code of Joomla, the class that implements a controller is named ''JController''. In order to save ourselves a lot of work, and prevent having to reinvent the wheel, this class can be extended to be used in our component.
* ''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 ==
With your favorite editor, create the following file
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


<span id="site/helloworld.php">
<span id="site/helloworld.php">
''site/helloworld.php''
'''<tt>site/helloworld.php</tt>'''
<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;


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


Line 32: Line 24:


// Redirect if set by the controller
// Redirect if set by the controller
$controller->redirect();</source>
$controller->redirect();
?>
 
</source>
</span>
</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 (it's a default behavior)


With your favorite file manager and editor, create a ''site/controller.php'' file containing
The static ''getInstance'' method of the ''JController'' class will create a controller. In the code above, it will create a controller named ''HelloWorldController'' using the <tt>controller.php</tt> file in the same location as the entry point. This means that we have to create this controller so that it has something to load. The call to ''redirect()'' on the controller instance will check if the task executed at some point set an url to redirect to after finishing execution, and if so, redirect to this url. This is useful for redirecting to a different view after executing a task.
 
With your favorite editor, create the following file


<span id="site/controller.php">
<span id="site/controller.php">
''site/controller.php''
'''<tt>site/controller.php</tt>'''
<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;


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


/**
* Hello World Component Controller
*/
class HelloWorldController extends JController
class HelloWorldController extends JController
{
{
}
}
?>
</source>
</source>
</span>
</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''.
Yes really, that is it. We do not have to provide any code in our controller except for this. This is because the default behavior of JController is to call the '''display''' task if no other task is given, and the '''display''' task will look for the name of the view to load in the '''view''' request variable. If no view name is given, it will load the view with the same name as the component, namely '''helloworld'''.
 
== Creating a view ==
 
Now that we have a controller that will load and display our views, we need to give it a view to display.


== Setting the view ==
With your favorite editor, create the following file
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">
<span id="site/views/helloworld/view.html.php">
''site/views/helloworld/view.html.php''
'''<tt>site/views/helloworld/view.html.php</tt>'''
<source lang="php">
<source lang="php" line>
<?php
<?php
// No direct access to this file
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
defined('_JEXEC') or die;


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


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


// Display the view
// Display the view
Line 87: Line 80:
}
}
}
}
?>
</source>
</source>
</span>
</span>


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
First of all, pay close attention to the naming of the class since that may be slightly confusing at first. The class is named '''HelloWorldViewHelloWorld''' and the reason '''HelloWorld''' appears twice is because it just happens to be the name of both our component and our view. A slightly better example would be for example '''FooViewBar''' where '''Foo''' would be the name of the component (like '''com_foo''') and '''Bar''' would be the name of the view.
 
Now, the most important two bits of this file are line 12, in which we assign a string to the member variable '''item''', this variable will then be available in the view template (which we will create in a minute). On line 15 there is a call to the <code>parent::display()</code> function, now this is in fact where a lot of the logic happens as that is the <code>display()</code> function of '''JView''', we will not go into detail on this except to tell you that it is responsible for loading up and displaying the view template.
 
The view templates are stored in the <tt>tmpl</tt> folder inside each view, so let us go ahead and create a view template for our view (View templates are also called layouts).
 
With your favorite editor, create the following file


<span id="site/views/helloworld/tmpl/default.php">
<span id="site/views/helloworld/tmpl/default.php">
''site/views/helloworld/tmpl/default.php''
'''<tt>site/views/helloworld/tmpl/default.php</tt>'''
<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;
 
?>
?>
<h1><?php echo $this->msg; ?></h1>
</source>


This template file will be included by the JView class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.
<h1><?php echo $this->item; ?></h1>


== Packaging the component ==
</source>


Content of your code directory
== Installation manifest ==
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_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!1.6_-_Part_01#index.html|site/views/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_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!1.6_-_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!1.6_-_Part_01#index.html|admin/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#admin/helloworld.php|admin/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/sql/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/sql/updates/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_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!1.6. You can test this basic component by putting ''index.php?option=com_helloworld'' in your browser address.
In our manifest, as you can see, we have added the newly created files and folders in the site files section on line 23 and 24. We have also updated the component version number.


<span id="helloworld.xml">
<span id="helloworld.xml">
''helloworld.xml''
'''<tt>helloworld.xml</tt>'''
<source lang="xml">
<source lang="xml" line>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
<extension type="component" version="2.5.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>June 2011</creationDate>
<author>John Doe</author>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorEmail>john.doe@example.org</authorEmail>
Line 139: Line 124:
<copyright>Copyright Info</copyright>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<license>License Info</license>
<!--  The version string is recorded in the components table -->
<!--  The version string is stored in the components table -->
<version>0.0.2</version>
<version>0.0.2</version>
<!-- The description is optional and defaults to the name -->
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<description>Description of the Hello World component ...</description>


<update> <!-- Runs on update; New in 1.6 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
in this section are copied from "site/" in the package -->
<files folder="site">
<files folder="site">
<filename>index.html</filename>
<filename>index.html</filename>
Line 162: Line 140:


<administration>
<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
in this section are copied from "admin/" in the package -->
<files folder="admin">
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</files>
</administration>
</administration>
Line 181: Line 154:
</span>
</span>


'''Result:'''
== Testing your component ==
You will see by default the message contained in the variable ''$this->msg'' in the ''view.html.php'' file.
 
For details on how to install the component into your Joomla! site, refer to the information provided in
[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#Testing_your_component|Part 01]].
 
In order to test this component, open up '''index.php?option=com_helloworld''' in your browser, and see that the text displayed will be the same text that you assigned to <code>$this->item</code> in your view.


== Zips ==
== File listing ==  
Download the zip file for this Part:
[http://www.leyar.com/joomlaorg/part02.zip]


== Navigate ==
* <tt>[[#helloworld.xml|helloworld.xml]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|site/index.html]]</tt>
* <tt>[[#site/helloworld.php|site/helloworld.php]]</tt>
* <tt>[[#site/controller.php|site/controller.php]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|site/views/index.html]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|site/views/helloworld/index.html]]</tt>
* <tt>[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|site/views/helloworld/tmpl/index.html]]</tt>
* <tt>[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/index.html]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#admin/helloworld.php|admin/helloworld.php]]</tt>


[[Developing a Model-View-Controller (MVC) Component for Joomla!1.6 - Part 01|Prev: Developing a Basic Component]] [[Developing a Model-View-Controller (MVC) Component for Joomla!1.6 - Part 03|Next: Adding a menu type to the site part]]
== Download this part ==


== Contributors ==
[https://github.com/downloads/rvsjoen/joomla-tutorials/com_helloworld-part02.zip Download example package]
*[[User:cdemko|Christophe Demko]]
 
*[[User:oaksu|Ozgur Aksu]]
== Articles in this series ==
{{:Chunks:Developing_an_MVC_Component_Contents}}


[[Category:Development]]
[[Category:Development]]
[[category:Joomla! 1.6]]
[[Category:Tutorials]]
[[category:Manual]]
[[Category:Joomla! 1.6]]
[[Category:Joomla! 1.7]]

Latest revision as of 15:06, 13 May 2012

Adding a view to the frontend

In order to create a basic view for the frontend, we first need to have some logic in our previously created entry point site/helloworld.php that decides how to handle requests which are passed to the component. We will manage this by having the entry point load up a controller and execute it, so the first order of business is telling the entry point what to do, and then creating a controller.

Creating a controller

In the core code of Joomla, the class that implements a controller is named JController. In order to save ourselves a lot of work, and prevent having to reinvent the wheel, this class can be extended to be used in our component.

With your favorite editor, create the following file

site/helloworld.php

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

jimport('joomla.application.component.controller');

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

// Perform the Request task
$controller->execute(JRequest::getCmd('task'));

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

The static getInstance 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 in the same location as the entry point. This means that we have to create this controller so that it has something to load. The call to redirect() on the controller instance will check if the task executed at some point set an url to redirect to after finishing execution, and if so, redirect to this url. This is useful for redirecting to a different view after executing a task.

With your favorite editor, create the following file

site/controller.php

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

jimport('joomla.application.component.controller');

class HelloWorldController extends JController
{

}
?>

Yes really, that is it. We do not have to provide any code in our controller except for this. This is because the default behavior of JController is to call the display task if no other task is given, and the display task will look for the name of the view to load in the view request variable. If no view name is given, it will load the view with the same name as the component, namely helloworld.

Creating a view

Now that we have a controller that will load and display our views, we need to give it a view to display.

With your favorite editor, create the following file

site/views/helloworld/view.html.php

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

jimport('joomla.application.component.view');

class HelloWorldViewHelloWorld extends JView
{
	function display($tpl = null) 
	{
		// Assign data to the view
		$this->item = 'Hello World';

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

First of all, pay close attention to the naming of the class since that may be slightly confusing at first. The class is named HelloWorldViewHelloWorld and the reason HelloWorld appears twice is because it just happens to be the name of both our component and our view. A slightly better example would be for example FooViewBar where Foo would be the name of the component (like com_foo) and Bar would be the name of the view.

Now, the most important two bits of this file are line 12, in which we assign a string to the member variable item, this variable will then be available in the view template (which we will create in a minute). On line 15 there is a call to the parent::display() function, now this is in fact where a lot of the logic happens as that is the display() function of JView, we will not go into detail on this except to tell you that it is responsible for loading up and displaying the view template.

The view templates are stored in the tmpl folder inside each view, so let us go ahead and create a view template for our view (View templates are also called layouts).

With your favorite editor, create the following file

site/views/helloworld/tmpl/default.php

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

?>

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

Installation manifest

In our manifest, as you can see, we have added the newly created files and folders in the site files section on line 23 and 24. We have also updated the component version number.

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>June 2011</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 stored 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>

	<!-- 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>
		<menu>Hello World!</menu>
		<!-- 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">
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
		</files>
	</administration>

</extension>

Testing your component

For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.

In order to test this component, open up index.php?option=com_helloworld in your browser, and see that the text displayed will be the same text that you assigned to $this->item in your view.

File listing

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5