Archived

Developing a MVC Component/Adding a view to the site part

From Joomla! Documentation

< Archived:Developing a MVC Component
Revision as of 04:13, 3 November 2009 by Cdemko (talk | contribs)

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.

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 it in our component. In the file site/helloworld.php (entry point of our Hello World component), put these lines

<?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
$controller->execute(JRequest::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 create a controller named HelloWorldController using the controller.php file

With your favorite files manager and editor, create a site/controller.php file containing

<?php
// No direct access to this file
defined( '_JEXEC' ) or die( 'Restricted access' );
// import Joomla controller library
jimport('joomla.application.component.controller');
// inherit the JController class
class HelloWorldController extends JController
{
}

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.

Setting the view[edit]

With your favorite files manager and editor, create a file site/views/helloworld/view.html.php able to display the default view and containing

// No direct access to this file
defined( '_JEXEC' ) or die( 'Restricted access' );
// import Joomla view library
jimport('joomla.application.component.view');
// inherit the JView class
class HelloWorldViewHelloWorld extends JView {
	protected $msg=null;
	function display($tpl = null) {
		$this->msg = 'Hello World';
		parent::display($tpl);
	}
}

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

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

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

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).