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.

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

class One extends SiteController
{
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\Component\SiteController will have the following properties available:

Auto-generation of views

The Hubzero\Component\SiteController automatically instantiates a new Hubzero\Component\View 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.

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.

Admin Controller

Administrator component controls are built and function the same as the Front-end (site) controllers with one key difference: they extends Hubzero\Component\AdminController.

<?php
class One extends AdminController
{
...
}

The primary difference between SiteController and AdminController is the pre-defining of a few tasks commonly used in administrator components.

API Controller

API controllers extend Hubzero\Component\ApiController. Functionally, API controllers are very similar to site and admin controllers in that defining executable tasks is done by creating public methods with a "Task" suffix. They differ, however, in two key ways:

2) The API has no concept of views and thus no View object to render data. Instead, data is sent back to the application via the send method which, in turn, prepares the response before delivering to the user.