API16

JController/ construct

From Joomla! Documentation

< API16:JController
Revision as of 17:35, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Constructor. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki> </span> ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The "API16" 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]

Constructor.

[Edit Descripton]

Template:Description:JController/ construct

Syntax[edit]

__construct($config=array())
Parameter Name Default Value Description
$config 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).

Defined in[edit]

libraries/joomla/application/component/controller.php

Importing[edit]

jimport( 'joomla.application.component.controller' );

Source Body[edit]

function __construct($config = array())
{
        // Initialize variables.
        $this->_redirect        = null;
        $this->_message         = null;
        $this->_messageType = 'message';
        $this->_taskMap         = array();
        $this->_methods         = array();
        $this->_data            = array();

        // Get the methods only for the final controller class
        $thisMethods    = get_class_methods(get_class($this));
        $baseMethods    = get_class_methods('JController');
        $methods                = array_diff($thisMethods, $baseMethods);

        // Add default display method
        $methods[] = 'display';

        // Iterate through methods and map tasks
        foreach ($methods as $method) {
                if (substr($method, 0, 1) != '_') {
                        $this->_methods[] = strtolower($method);
                        // auto register public methods as tasks
                        $this->_taskMap[strtolower($method)] = $method;
                }
        }

        //set the view name
        if (empty($this->_name)) {
                if (array_key_exists('name', $config))  {
                        $this->_name = $config['name'];
                } else {
                        $this->_name = $this->getName();
                }
        }

        // Set a base path for use by the controller
        if (array_key_exists('base_path', $config)) {
                $this->_basePath        = $config['base_path'];
        } else {
                $this->_basePath        = JPATH_COMPONENT;
        }

        // If the default task is set, register it as such
        if (array_key_exists('default_task', $config)) {
                $this->registerDefaultTask($config['default_task']);
        } else {
                $this->registerDefaultTask('display');
        }

        // set the default model search path
        if (array_key_exists('model_path', $config)) {
                // user-defined dirs
                $this->addModelPath($config['model_path']);
        } else {
                $this->addModelPath($this->_basePath.DS.'models');
        }

        // set the default view search path
        if (array_key_exists('view_path', $config)) {
                // user-defined dirs
                $this->_setPath('view', $config['view_path']);
        } else {
                $this->_setPath('view', $this->_basePath.DS.'views');
        }
}

[Edit See Also] Template:SeeAlso:JController/ construct

Examples[edit]

<CodeExamplesForm />