JController/getInstance
From Joomla! Documentation
< API15:JControllerRevision as of 08:53, 12 May 2013 by JoomlaWikiBot (talk | contribs) (removing red link to edit, no existant pages)
Revision as of 08:53, 12 May 2013 by JoomlaWikiBot (talk | contribs) (removing red link to edit, no existant pages)
The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Method to get a singleton controller instance.
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax[edit]
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[edit]
mixed derivative class or JException on error.
Defined in[edit]
libraries/joomla/application/component/controller.php
Importing[edit]
jimport( 'joomla.application.component.controller' );
Source Body[edit]
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 edit link to red link >] <! removed transcluded page call, red link never existed >
Examples[edit]
<CodeExamplesForm />