API16:JController/getInstance
From Joomla! Documentation
Description
Method to get a singleton controller instance.
<! removed transcluded page call, red link never existed >
Syntax
static getInstance($prefix, $config=array())
Parameter Name | Default Value | Description |
---|---|---|
$prefix | $name The prefix for the controller. | |
$config | array() | $config An array of optional constructor options. |
Returns
mixed derivative class or on error.
Defined in
libraries/joomla/application/component/controller.php
Importing
jimport( 'joomla.application.component.controller' );
Source Body
public static function getInstance($prefix, $config = array())
{
static $instance;
if (!empty($instance)) {
return $instance;
}
// Get the environment configuration.
$basePath = array_key_exists('base_path', $config) ? $config['base_path'] : JPATH_COMPONENT;
$protocol = JRequest::getWord('protocol');
$command = JRequest::getCmd('task', 'display');
// Check for a controller.task command.
if (strpos($command, '.') !== false) {
// Explode the controller.task command.
list($type, $task) = explode('.', $command);
// Define the controller filename and path.
$file = self::_createFileName('controller', array('name' => $type, 'protocol' => $protocol));
$path = $basePath.DS.'controllers'.DS.$file;
// Reset the task without the contoller context.
JRequest::setVar('task', $task);
} else {
// Base controller.
$type = null;
$task = $command;
// Define the controller filename and path.
$file = self::_createFileName('controller', array('name' => 'controller', 'protocol' => $protocol));
$path = $basePath.DS.$file;
}
// Get the controller class name.
$class = ucfirst($prefix).'Controller'.ucfirst($type);
// Include the class if not present.
if (!class_exists($class)) {
// If the controller file path exists, include it.
if (file_exists($path)) {
require_once $path;
} else {
throw new JException(JText::sprintf('INVALID CONTROLLER', $type), 1056, E_ERROR, $type, true);
}
}
// Instantiate the class.
if (class_exists($class)) {
$instance = new $class($config);
} else {
throw new JException(JText::sprintf('INVALID CONTROLLER CLASS', $class), 1057, E_ERROR, $class, true);
}
return $instance;
}
<! removed transcluded page call, red link never existed >
Examples
<CodeExamplesForm />