Source code for file /joomla/application/component/controller.php
Documentation is available at controller.php
* @version $Id: controller.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package Joomla.Framework
* @subpackage Application
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* Base class for a Joomla Controller
* Controller (controllers are where you put all the actual code) Provides basic
* functionality, such as rendering views (aka displaying templates).
* @package Joomla.Framework
* @subpackage Application
* @author Johan Janssens <johan.janssens@joomla.org>
* @author Louis Landry <louis.landry@joomla.org>
* The base path of the controller
* The name of the controller
* Array of class methods to call for a given task.
* Current or most recent task to be performed.
* The mapped task that was performed.
* The set of search directories for resources (views).
* ACO Section for the controller.
* Default ACO Section value for the controller.
* @param array An optional associative array of configuration settings.
* Recognized key values include 'name', 'default_task', 'model_path', and
* 'view_path' (this list is not meant to be comprehensive).
//Initialize private variables
// Get the methods only for the final controller class
$methods =
array_diff( $thisMethods, $baseMethods );
// Add default display method
// Iterate through methods and map tasks
foreach ( $methods as $method )
if ( substr( $method, 0, 1 ) !=
'_' ) {
// auto register public methods as tasks
if (empty( $this->_name ))
$this->_name =
$config['name'];
// Set a base path for use by the controller
// If the default task is set, register it as such
// set the default model search path
// set the default view search path
$this->_setPath( 'view', $config['view_path'] );
* Execute a task by triggering a method in the derived class.
* @param string The task to perform. If no matching task is found, the
* '__default' task is executed, if defined.
* @return mixed|falseThe value returned by the called method, false in
} elseif (isset
( $this->_taskMap['__default'] )) {
// Record the actual task being fired
// Make sure we have access
$retval =
$this->$doTask();
* @param string $task The ACO Section Value to check access on
* @return boolean True if authorized
// Only do access check if the aco section is set
// If we have a section value set that trumps the passed task ???
// We have one, so set it and lets do the check
// Get the JUser object for the current user and return the authorization boolean
// Nothing set, nothing to check... so obviously its ok :)
* Typical view method for MVC based architecture
* This function is provide as a default implementation, in most cases
* you will need to override it in your own controllers.
* @param string $cachable If true, the view output will be cached
$viewType =
$document->getType();
$view =
& $this->getView( $viewName, $viewType, '', array( 'base_path'=>
$this->_basePath));
if ($model =
& $this->getModel($viewName)) {
// Push the model into the view (as default)
$view->setModel($model, true);
$view->setLayout($viewLayout);
$cache->get($view, 'display');
* Redirects the browser or returns false if no redirect is set.
* @return boolean False if no redirect exists.
* Method to get a model object, loading it if required.
* @param string The model name. Optional.
* @param string The class prefix. Optional.
* @param array Configuration array for model. Optional.
* @return object The model.
function &getModel( $name =
'', $prefix =
'', $config =
array() )
if ( empty( $prefix ) ) {
$prefix =
$this->getName() .
'Model';
if ( $model =
& $this->_createModel( $name, $prefix, $config ) )
// task is a reserved state
$model->setState( 'task', $this->_task );
// Lets get the application object and set menu information if its available
$menu =
&$app->getMenu();
if ($item =
$menu->getActive())
$params =
& $menu->getParams($item->id);
// Set Default State Data
$model->setState( 'parameters.menu', $params );
* Adds to the stack of model paths in LIFO order.
* @param string|arrayThe directory (string), or list of directories
jimport('joomla.application.component.model');
* Gets the available tasks in the controller.
* @return array Array[i] of task names.
* Get the last task that is or was to be performed.
* @return string The task that was or is being performed.
* Method to get the controller name
* The dispatcher name by default parsed using the classname, or it can be set
* by passing a $config['name'] in the class constructor
* @return string The name of the dispatcher
JError::raiseError(500, "JController::getName() : Cannot get or parse class name.");
* Method to get a reference to the current view and load it if necessary.
* @param string The view name. Optional, defaults to the controller
* @param string The view type. Optional.
* @param string The class prefix. Optional.
* @param array Configuration array for view. Optional.
* @return object Reference to the view or an error.
function &getView( $name =
'', $type =
'', $prefix =
'', $config =
array() )
if ( !isset
( $views ) ) {
if ( empty( $prefix ) ) {
$prefix =
$this->getName() .
'View';
if ( empty( $views[$name] ) )
if ( $view =
& $this->_createView( $name, $prefix, $type, $config ) ) {
500, JText::_( 'View not found [name, type, prefix]:' )
.
' ' .
$name .
',' .
$type .
',' .
$prefix
* Add one or more view paths to the controller's stack, in LIFO order.
* @param string|arrayThe directory (string), or list of directories