API15

JController/ construct: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Constructor. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki> </span> ...
 
Doxiki (talk | contribs)
No edit summary
Line 29: Line 29:
===Source Body===
===Source Body===
<source lang="php">
<source lang="php">
function __construct($config = array())
function __construct( $config = array() )
{
{
         // Initialize variables.
         //Initialize private variables
         $this->_redirect        = null;
         $this->_redirect        = null;
         $this->_message        = null;
         $this->_message        = null;
Line 40: Line 40:


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


         // Add default display method
         // Add default display method
Line 48: Line 48:


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


         //set the view name
         //set the view name
         if (empty($this->_name)) {
         if (empty( $this->_name ))
        {
                 if (array_key_exists('name', $config))  {
                 if (array_key_exists('name', $config))  {
                         $this->_name = $config['name'];
                         $this->_name = $config['name'];
Line 73: Line 75:


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


         // set the default model search path
         // set the default model search path
         if (array_key_exists('model_path', $config)) {
         if ( array_key_exists( 'model_path', $config ) ) {
                 // user-defined dirs
                 // user-defined dirs
                 $this->addModelPath($config['model_path']);
                 $this->addModelPath($config['model_path']);
Line 88: Line 90:


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

Revision as of 22:09, 22 March 2010

Description

Constructor.

[Edit Descripton]

Template:Description:JController/ construct

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' );
        }
}

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

Examples

<CodeExamplesForm />