Actions

User

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

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
(Creating a controller)
(Creating a view)
 
Line 84: Line 84:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
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.
 
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.

Latest revision as of 10:06, 13 May 2012

Contents

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

  1. <?php
  2. // No direct access to this file
  3. defined('_JEXEC') or die;
  4.  
  5. jimport('joomla.application.component.view');
  6.  
  7. class HelloWorldViewHelloWorld extends JView
  8. {
  9.         function display($tpl = null) 
  10.         {
  11.                 // Assign data to the view
  12.                 $this->item = 'Hello World';
  13.  
  14.                 // Display the view
  15.                 parent::display($tpl);
  16.         }
  17. }
  18. ?>

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <extension type="component" version="2.5.0" method="upgrade">
  3.  
  4.         <name>Hello World!</name>
  5.         <!-- The following elements are optional and free of formatting constraints -->
  6.         <creationDate>June 2011</creationDate>
  7.         <author>John Doe</author>
  8.         <authorEmail>john.doe@example.org</authorEmail>
  9.         <authorUrl>http://www.example.org</authorUrl>
  10.         <copyright>Copyright Info</copyright>
  11.         <license>License Info</license>
  12.         <!--  The version string is stored in the components table -->
  13.         <version>0.0.2</version>
  14.         <!-- The description is optional and defaults to the name -->
  15.         <description>Description of the Hello World component ...</description>
  16.  
  17.         <!-- Note the folder attribute: This attribute describes the folder
  18.                 to copy FROM in the package to install therefore files copied
  19.                 in this section are copied from "site/" in the package -->
  20.         <files folder="site">
  21.                 <filename>index.html</filename>
  22.                 <filename>helloworld.php</filename>
  23.                 <filename>controller.php</filename>
  24.                 <folder>views</folder>
  25.         </files>
  26.  
  27.         <administration>
  28.                 <menu>Hello World!</menu>
  29.                 <!-- Note the folder attribute: This attribute describes the folder
  30.                         to copy FROM in the package to install therefore files copied
  31.                         in this section are copied from "admin/" in the package -->
  32.                 <files folder="admin">
  33.                         <filename>index.html</filename>
  34.                         <filename>helloworld.php</filename>
  35.                 </files>
  36.         </administration>
  37.  
  38. </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