API15

JController/ construct: Difference between revisions

From Joomla! Documentation

m removing red link to edit, no existant pages
m preparing for archive only
 
Line 105: Line 105:


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=__construct
  category=__construct
  category=JController
  category=JController
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*

Latest revision as of 00:24, 25 March 2017

Description

Constructor.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

__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

libraries/joomla/application/component/controller.php

Importing

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

Source Body

function __construct( $config = array() )
{
        //Initialize private 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' );
        }
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

Code Examples