From Joomla! Documentation

< API16:JController
Revision as of 17:35, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.


Method to get a singleton controller instance.

[Edit Descripton]



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.


mixed derivative class or on error.

Defined in



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;

[Edit See Also] SeeAlso:JController/getInstance


<CodeExamplesForm />