Actions

API16

JController/getInstance

From Joomla! Documentation

< API16:JController
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.

Contents

Description

Method to get a singleton controller instance.

[<! removed edit link to red link >]

<! 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 edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />