Source code for file /joomla/application/component/controller.php
Documentation is available at controller.php
* @version $Id: controller.php 6631 2007-02-15 03:39:09Z eddiea $
* @package Joomla.Framework
* @subpackage Application
* @copyright Copyright (C) 2005 - 2007 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
* 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 or models).
* 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
//Set the controller name
if ( empty( $this->_name ) )
if ( isset
( $config['name'] ) )
$this->_name =
$config['name'];
'JController::__construct() :'
.
' Can\'t get or parse class name.'
// If the default task is set, register it as such
if ( isset
( $config['default_task'] ) ) {
// set the default model search path
if ( isset
( $config['model_path'] ) ) {
$this->_setPath( 'model', $config['model_path'] );
// set the default view search path
if ( isset
( $config['view_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
// We have a method in the map to this task
} elseif (isset
( $this->_taskMap['__default'] )) {
// Didn't find the method, but we do have a default method
// Don't have a default method either...
// Record the actual task being fired
// Time to make sure we have access to do what we want to do...
// Yep, lets do it already
// No access... better luck next time
* @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
$viewType =
$document->getType();
$view =
& $this->getView( $viewName, $viewType);
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.
* @param string The class prefix. Optional.
* @return object The model.
function &getModel( $name, $prefix =
'' )
if ( empty( $prefix ) ) {
$prefix =
$this->_name .
'Model';
if ( $model =
& $this->_createModel( $name, $prefix ) )
// task is a reserved state
$model->setState( 'task', $this->_task );
// Get menu item information if Itemid exists
if ($item =
$menu->getActive())
$params =
& $menu->getParams($item->id);
// Set Default State Data
$model->setState( 'parameters.menu', $params );
* Adds to the stack of controller model paths in LIFO order.
* @param string|arrayThe directory (string), or list of directories
* 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 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.
* @return object Reference to the view or an error.
function &getView( $name =
'', $type =
'', $prefix =
'' )
if ( !isset
( $views ) ) {
if ( empty( $prefix ) ) {
$prefix =
$this->_name .
'View';
if ( empty( $views[$name] ) ) {
if ( $view =
& $this->_createView( $name, $prefix, $type ) ) {
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
* Register (map) a task to a method in the class.
* @param string The task.
* @param string The name of the method in the derived class to perform
* Register the default task to perform if a mapping is not found.
* @param string The name of the method in the derived class to perform if
* a named task is not found.
* @return string The error message.
* @param string The error message.
* @return string The new error message.
* Sets the internal message that is passed with a redirect
* @param string The message
* Set a URL for browser redirection.
* @param string URL to redirect to.
* @param string Message to display on redirect. Optional, defaults to
* value set internally by controller, if any.
* @param string Message type. Optional, defaults to 'message'.
function setRedirect( $url, $msg =
null, $type =
'message' )
// controller may have set this directly
* Sets the access control levels.
* @param string The ACO section (eg, the component).
* @param string The ACO section value (if using a constant value).
* Method to load and return a model object.
* @param string The name of the model.
* @param string Optional model prefix.
* @return mixed Model object on success; error object or null on
function &_createModel( $name, $prefix =
'')
// Build the model class name
$modelClass =
$classPrefix .
$modelName;
jimport( 'joomla.filesystem.path' );
$this->_createFileName( 'model', array( 'name' =>
$modelName ) )
JText::_( 'Model class not found [class, file]:' )
.
' ' .
$modelClass .
', ' .
$path
$result =
new $modelClass();
* Method to load and return a view object. This method first looks in the
* current template directory for a match, and failing that uses a default
* set path to load the view class file.
* Note the "name, prefix, type" order of parameters, which differs from the
* "name, type, prefix" order used in related public methods.
* @param string The name of the view.
* @param string Optional prefix for the view class name.
* @return mixed View object on success; null or error result on failure.
function &_createView( $name, $prefix =
'', $type =
'' )
// Build the view class name
$viewClass =
$classPrefix .
$viewName;
jimport( 'joomla.filesystem.path' );
$this->_createFileName( 'view', array( 'name' =>
$viewName, 'type' =>
$viewType) )
500, JText::_( 'View class not found [class, file]:' )
.
' ' .
$viewClass .
', ' .
$path );
$result =
new $viewClass();
* Sets an entire array of search paths for resources.
* @param string The type of path to set, typically 'view' or 'model'.
* @param string|array The new set of search paths. If null or false,
* resets to the current directory only.
// clear out the prior search dirs
$this->_path[$type] =
array();
// always add the fallback directories as last resort
$this->_addPath( $type, JPATH_COMPONENT .
DS .
'views' );
$this->_addPath( $type, JPATH_COMPONENT .
DS .
'models' );
// actually add the user-specified directories
* Adds to the search path for templates and resources.
* @param string The path type (e.g. 'model', 'view'.
* @param string|arrayThe directory or stream to search.
// just force path to array
// loop through the path directories
foreach ( $path as $dir )
// no surrounding spaces allowed!
// add trailing separators as needed
if ( substr( $dir, -
1 ) !=
DIRECTORY_SEPARATOR ) {
$dir .=
DIRECTORY_SEPARATOR;
// add to the top of the search dirs
* Create the filename for a resource.
* @param string The resource type to create the filename for.
* @param array An associative array of filename information. Optional.
* @return string The filename.
function _createFileName( $type, $parts =
array() )
if ( !empty( $parts['type'] ) ) {
$parts['type'] =
'.'.
$parts['type'];
$filename =
strtolower($parts['name']).
DS.
'view'.
$parts['type'].
'.php';