Documentation

Controllers

Overview

The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.

Most components have two controllers: one for the front-end and one for the back-end.

Creating the Front-end Controller

Most HUBzero component controllers will differ from Joomla! in some important ways. Some changes are made to aid in development while others may simply be a difference in philosophy. Note, however, that no differences require hacking or altering Joomla! in any way and HUBzero methodologies will run on any stock Joomla! install.

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

ximport('Hubzero_Controller');

class HelloControllerOne extends Hubzero_Controller
{
	public function displayTask() 
	{
		// Pass the view any data it may need
		$this->view->greeting = 'Hello, World!';
		
		// Set any errors
		if ($this->this->getError()) 
		{
			foreach ($this->getErrors() as $error)
			{
				$view->setError($error);
			}
		}
		
		// Output the HTML
		$this->view->display();
	}
}

There doesn't appear to be much going on here due to some of the auto-setup the Hubzero_Controller class brings.

The first, and most important, difference to note is that we're extending Hubzero_Controller rather than JController. Since we're not employing JController, how tasks and views are determined, built, and executed differ from standard Joomla components.

Note: Hubzero_Controller extends JObject, so all its methods and properties are available.

One key difference is how the execute() method is handled. In Joomla!, any public method is assumed to be an executable task. In the HUBzero method, the list of available tasks is built from only methods that are 1) public and 2) end in "Task". When calling a task, the "Task" suffix should be left off. For example:

// This route
JRoute::_('index.php?option=com_example&task=other');

// Refers to 
....
public function otherTask()
{
	...
}
....

If no task is supplied, the controller will default to a task of "display". The default task can be set in the controller:

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

ximport('Hubzero_Controller');

class HelloControllerOne extends Hubzero_Controller
{
	public function execute() 
	{
		// Set the default task
		$this->registerTask('__default', 'mydefault');

		// Set the method to execute for other tasks
		// The following can be called by task=delete and will execute the removeTask method
		$this->registerTask('delete', 'remove');  // (task, method name);

		parent::execute();
	}
	...
}

Each controller extending Hubzero_Controller will have the following properties available:

  • _option - String, component name (e.g., com_example)
  • _controller - String, controller name
  • view - Object (JView)
  • config - Object (JParamter), component config
  • database - Object (JDatabase)
  • juser - Object (JUser)
class HelloControllerOne extends Hubzero_Controller
{
	public function displayTask() 
	{
		$this->view->userName = $this->juser->get('name');
		$this->view->display();
	}
}

Auto-generation of views

The Hubzero_Controller automatically instantiates a new JView object for each task and assigns the component ($option) and controller ($controller) names as properties for use in your view. Controller names map to view directory and task names directly map to view names.

  /{component}
    /views
      /one  (controller name)
        /tmpl
          /display.php
          /remove.php

Example usage within a view:

<p>This is component <?php echo $this->option; ?> using controller: <?php echo $this->controller; ?></p>

Changing view layout

As mentioned above, the view object is auto-generated with the same layout as the current $task. There are times, however, when you may want to use a different layout or are executing a task after directing through from a previous task (example: saveTask encountering an error and falling through to the editTask to display the edit form with error message). The layout can easily be switched with the setLayout method.

  /{component}
    /views
      /one  (controller name)
        /tmpl
          /display.php
          /world.php

//-------------
//-------------

class HelloControllerOne extends Hubzero_Controller
{
	public function displayTask() 
	{
		// Set the layout to 'world.php'
		$this->view->setLayout('world');
		
		// Output the HTML
		$this->view->display();
	}
}

Any assigned data or vars to the view will not be effected.

Last modified:

  • Copyright © 2022 Hubzero
  • Powered by Hubzero®