https://docs.joomla.org/api.php?action=feedcontributions&user=Jsharris1229&feedformat=atomJoomla! Documentation - User contributions [en]2024-03-29T05:01:23ZUser contributionsMediaWiki 1.35.10https://docs.joomla.org/index.php?title=Archived:Developing_a_MVC_Component/Adding_a_view_to_the_site_part&diff=66751Archived:Developing a MVC Component/Adding a view to the site part2012-04-19T23:54:47Z<p>Jsharris1229: /* Setting the view */</p>
<hr />
<div>This tutorial is for {{JVer|1.6}} {{JVer|1.7}} {{JVer|2.5}}<br />
<br />
== Articles in this series ==<br />
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Contents}}<br />
<br />
== Introduction ==<br />
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.<br />
<br />
In the Joomla!2.5 framework, third party component authors divide their code into three main parts:<br />
* ''models'' They manage the data <br />
* ''controllers'' They perform tasks, set and get the states of the models and ask the views to display<br />
* ''views'' They display the content according to the type (''error'', ''feed'', ''html'', ''json'', ''raw'', ''xml'') and the layout chosen by the controllers<br />
<br />
== Setting the controller ==<br />
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<br />
<br />
<span id="site/helloworld.php"><br />
''site/helloworld.php''<br />
<source lang="php"><br />
<?php<br />
// No direct access to this file<br />
defined('_JEXEC') or die('Restricted access');<br />
<br />
// import joomla controller library<br />
jimport('joomla.application.component.controller');<br />
<br />
// Get an instance of the controller prefixed by HelloWorld<br />
$controller = JController::getInstance('HelloWorld');<br />
<br />
// Perform the Request task<br />
$controller->execute(JRequest::getCmd('task'));<br />
<br />
// Redirect if set by the controller<br />
$controller->redirect();</source><br />
</span><br />
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).<br />
<br />
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<br />
<br />
<span id="site/controller.php"><br />
''site/controller.php''<br />
<source lang="php"><br />
<?php<br />
// No direct access to this file<br />
defined('_JEXEC') or die('Restricted access');<br />
<br />
// import Joomla controller library<br />
jimport('joomla.application.component.controller');<br />
<br />
/**<br />
* Hello World Component Controller<br />
*/<br />
class HelloWorldController extends JController<br />
{<br />
}<br />
</source><br />
</span><br />
<br />
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''.<br />
<br />
== Setting the view ==<br />
<br />
When JController wants to display a view, it will look for certain files in the ''component/com_[component_name]/views/[name of view]/'' folder. <br />
<br />
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/''.<br />
<br />
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''.<br />
<br />
With your favorite file manager and editor, create a file ''site/views/helloworld/view.html.php'' able to display the default view and containing<br />
<br />
<span id="site/views/helloworld/view.html.php"><br />
''site/views/helloworld/view.html.php''<br />
<source lang="php"><br />
<?php<br />
// No direct access to this file<br />
defined('_JEXEC') or die('Restricted access');<br />
<br />
// import Joomla view library<br />
jimport('joomla.application.component.view');<br />
<br />
/**<br />
* HTML View class for the HelloWorld Component<br />
*/<br />
class HelloWorldViewHelloWorld extends JView<br />
{<br />
// Overwriting JView display method<br />
function display($tpl = null) <br />
{<br />
// Assign data to the view<br />
$this->msg = 'Hello World';<br />
<br />
// Display the view<br />
parent::display($tpl);<br />
}<br />
}<br />
</source><br />
</span><br />
<br />
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<br />
<br />
<span id="site/views/helloworld/tmpl/default.php"><br />
''site/views/helloworld/tmpl/default.php''<br />
<source lang="php"><br />
<?php<br />
// No direct access to this file<br />
defined('_JEXEC') or die('Restricted access');<br />
?><br />
<h1><?php echo $this->msg; ?></h1><br />
</source><br />
<br />
This template file will be included by the JView class. Therefore, here, $this refers to the HelloWorldViewHelloWorld class.<br />
<br />
== Packaging the component ==<br />
<br />
Content of your code directory<br />
* ''[[#helloworld.xml|helloworld.xml]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/index.html]]''<br />
* ''[[#site/helloworld.php|site/helloworld.php]]''<br />
* ''[[#site/controller.php|site/controller.php]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/index.html]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/index.html]]''<br />
* ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''<br />
* ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/index.html]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#admin/helloworld.php|admin/helloworld.php]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/index.html]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/index.html]]''<br />
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''<br />
* ''[[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]]''<br />
<br />
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.<br />
<br />
<span id="helloworld.xml"><br />
''helloworld.xml''<br />
<source lang="xml"><br />
<?xml version="1.0" encoding="utf-8"?><br />
<extension type="component" version="2.5.0" method="upgrade"><br />
<br />
<name>Hello World!</name><br />
<!-- The following elements are optional and free of formatting constraints --><br />
<creationDate>November 2009</creationDate><br />
<author>John Doe</author><br />
<authorEmail>john.doe@example.org</authorEmail><br />
<authorUrl>http://www.example.org</authorUrl><br />
<copyright>Copyright Info</copyright><br />
<license>License Info</license><br />
<!-- The version string is recorded in the components table --><br />
<version>0.0.2</version><br />
<!-- The description is optional and defaults to the name --><br />
<description>Description of the Hello World component ...</description><br />
<br />
<update> <!-- Runs on update; New in 2.5 --><br />
<schemas><br />
<schemapath type="mysql">sql/updates/mysql</schemapath><br />
</schemas><br />
</update><br />
<br />
<!-- Site Main File Copy Section --><br />
<!-- Note the folder attribute: This attribute describes the folder<br />
to copy FROM in the package to install therefore files copied<br />
in this section are copied from /site/ in the package --><br />
<files folder="site"><br />
<filename>index.html</filename><br />
<filename>helloworld.php</filename><br />
<filename>controller.php</filename><br />
<folder>views</folder><br />
</files><br />
<br />
<administration><br />
<!-- Administration Menu Section --><br />
<menu>Hello World!</menu><br />
<!-- Administration Main File Copy Section --><br />
<!-- Note the folder attribute: This attribute describes the folder<br />
to copy FROM in the package to install therefore files copied<br />
in this section are copied from /admin/ in the package --><br />
<files folder="admin"><br />
<!-- Admin Main File Copy Section --><br />
<filename>index.html</filename><br />
<filename>helloworld.php</filename><br />
<!-- SQL files section --><br />
<folder>sql</folder><br />
</files><br />
</administration><br />
<br />
</extension><br />
</source><br />
</span><br />
<br />
'''Result:'''<br />
You will see by default the message contained in the variable ''$this->msg'' in the ''view.html.php'' file.<br />
<br />
== Zips ==<br />
Download the zip file for this Part:<br />
[http://www.leyar.com/joomlaorg/part02.zip]<br />
<br />
== Navigate ==<br />
<br />
[[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]]<br />
<br />
== Contributors ==<br />
*[[User:cdemko|Christophe Demko]]<br />
*[[User:oaksu|Ozgur Aksu]]<br />
<br />
[[Category:Development]]<br />
[[category:Joomla! 1.6]]<br />
[[category:Joomla! 1.7]]<br />
[[category:Joomla! 2.5]]<br />
[[category:Manual]]</div>Jsharris1229