Book Image

Mastering Joomla! 1.5 Extension and Framework Development Second Edition

Book Image

Mastering Joomla! 1.5 Extension and Framework Development Second Edition

Overview of this book

Joomla! is one of the world’s top open source content management systems, which enables you to build websites and powerful online applications. Out of the box, Joomla! does a great job of managing the content needed to enhance the functionality of your website. But for many people, the true power of Joomla! lies in its application framework that makes it possible for thousands of developers around the world to create powerful add-ons and extensions. This book will guide you through the complexities of implementing add-ons, components, modules, and plugins in Joomla! 1.5. If you feel that you’ve mastered the basics of creating Joomla! extensions, then this book will take you to the next level. Packed with expert advice on all aspects of programming with Joomla!, this book is an invaluable reference guide you will turn to again and again for your Joomla! development tasks. The book starts by explaining the structure and design of the Joomla! Framework. Then we move on to extending and storing data in standard fields and dealing with multilingual requirements. Further, you will discover best practices, design, and coding methods for Joomla! components, modules, plugins, and other extensions. Along the way, you will actually design and create a component, module, and plugin that work together to add functionality to the Joomla! framework. You will also learn about customizing the page output using JavaScript effects, making use of Web Services from within Joomla! and ensuring that your code is secure and error-free. You will discover how to easily create sophisticated extensions that are robust, user-friendly, and professional by taking advantage of the many libraries and utilities provided by the Joomla! framework. A comprehensive, up-to-date reference to the Joomla! Framework API is also included in the book. Finally, we will discuss advanced methods for rendering your extensions more robust, secure, and professional.
Table of Contents (18 chapters)
Mastering Joomla! 1.5 Extension and Framework Development
Credits
About the Author
About the Reviewer
Preface

JController


abstract, extends JObject , located in / joomla/application/component/controller.php

This is the base controller class used in MVC implementations. Controllers provide basic functionality such as rendering views. For more information about the JController class refer to Chapter 5, Component Design.

Properties

string $_acoSection

ACO section for the controller

string $_acoSectionValue

Default ACO section value for the controller

string $_basePath

The base path of the controller

string $_doTask

Task method that was executed

string $_message

Message to include in redirect

string $_messageType

Type of message to include in redirect

array $_methods

Array of class methods

string $_name

The name of the controller

array $_path

Set of search directories for resources (views, models)

string $_redirect

URL for redirection

string $_task

Current or most recent task to be executed

array $_taskmap

Array of class methods to call for a given task

Inherited properties

Inherited from JObject:

  • JObject::_errors

Inherited methods

Inherited from JObject:

  • JObject::JObject()

  • JObject::__construct()

  • JObject::get()

  • JObject::getError()

  • JObject::getErrors()

  • JObject::getProperties()

  • JObject::getPublicProperties()

  • JObject::set()

  • JObject::setError()

  • JObject::setProperties()

  • JObject::toString()

Methods

Constructor __construct

Builds a new JController object. $config, an associative array, can contain the keys name, base_path, default_task, model_path, and view_path.

  • name becomes the controller name, unless the controller name is already defined by the subclass

  • default_task is the task that will be executed by default (this is not the same as the method)

  • model_path is the JModel subclass search path, prepended by JPATH_COMPONENT

  • view_path is the JView subclass search path, prepended by JPATH_COMPONENT

Redefines JObject::__construct(); overridden in descendant classes.

JController __construct([$config = null])

  • array $config: An optional associative array of configuration settings

addModelPath

This method adds one or more JModel paths to the controller's stack in LIFO order. $path can be a string or an array of strings.

void addModelPath($path)

  • mixed $path: The directory (string) or an array of directories to add

  • void: No return type

addViewPath

This method adds one or more JView paths to the controller's stack in LIFO order. $path can be a string or an array of strings.

void addViewPath($path)

  • mixed $path: The directory (string) or array of directories to add

  • void: No return type

authorize

If $_acoSection has not been set, authorization is automatically granted. If $_acoSection has been set and $_acoSectionValue has been set then $_acoSectionValue is assigned to $task. The method then determines if the current user has the rights to complete the specified $task.

boolean authorize($task)

  • string $task: The directory (string) or list of directories (array) to add

  • boolean: Returns true is authorized, false if not authorized

display

This method is provided as a default implementation; derived controller classes will normally override this method. The method uses JRequest (view and layout) to determine the view name and the template layout to use. If the view is not known then the controller name is used. layout determines which template to use, normally default. If cachable is true then the global cache object is used to get and populate the display cache.

void display([$cachable = false])

  • boolean $cachable: Use global cache object if true

  • void: No return type

execute

This method executes a task by triggering a method in a descendant class. If a mapped method for $task does not exist, it attempts to execute the default $task. If a mapped method for the default $task does not exist, a 404 error is raised. When a mapped method is found, access rights are checked using the authorize() method. If access is denied, a 403 error is raised.

mixed execute($task)

  • string $task: The task to execute

  • mixed: Returns the mapped method or an error if no mapped method exists

getModel

This method returns a reference to a JModel subclass object, creating a new instance if one does not exist. If $prefix is not specified, the name of the controller concatenated with the word 'Model' is used. $name is the model class name suffix. If the class does not exist, Joomla! will attempt to load it from the model paths. If the file where the class is expected to reside is found but the class is missing, an error will be thrown.

JModel &getModel([$name = ''], [$prefix = ''], [$config = array()])

  • string $name: Optional model name

  • string $prefix: Optional class prefix

  • array $config: Optional configuration array for the model

  • object: Returns a reference to a JModel subclass object

getName

This method returns the name of the controller.

string getName()

  • string: Returns the controller name

getTask

This method returns the current task or the last task that was executed.

string getTask()

  • strin g: Returns the current task or the last task that was executed

getTasks

This method gets a list of the available tasks in the controller and returns an array of task names.

array getTasks()

  • array: Returns an array of task names

getView

This method returns a reference to a JView subclass object creating a new instance if one does not exist. If $name is not specified, the controller name is used. If $prefix is not specified, the prefix will be $controllerName.'View'. $type is the view layout, normally 'html', but can have a value of 'feed', 'html', 'pdf', 'raw', or 'error'. This method is normally called with only $name specified, for example:

    $view = $SomeController->getView('Item');

This would attempt to instantiate the JView class SomeViewItem.

JView &getView([$name = ''], [$type = ''], [$prefix = ''], [$config = array()])

  • string $name: Optional view name, defaults to the controller name

  • string $type: Optional view type

  • string $prefix: Optional class prefix

  • array $config: Optional configuration array for the view

  • object: Returns a reference to a JView subclass object

redirect

If a redirect has been set, this method redirects the browser and closes the application.

boolean redirect()

  • boolean: Returns false if no redirect exists

registerDefaultTask

This method registers the default task. The default task is the task that is executed when an attempt is made to execute a task that is not mapped to a method.

void registerDefaultTask($method)

  • string $method: Derived class name to perform if a named task is not found.

  • void: No return.

registerTask

This method registers (maps) a task to a method in the class.

void registerTask($task, $method)

  • string $task: The task name

  • string $method: Derived class method name to perform for the task

  • void: No return type

setAccessControl

This method sets the object authorization ACO section and ACO value. This is used by the authorize() method.

void setAccessControl($section, [$value = null])

  • string $section: The ACO section (the component for example)

  • string $value: Optional ACO section value (if using a constant value)

  • void: No return type

setMessage

This method sets the internal message that is passed with a redirect.

string setMessage($text)

  • string $text: The new message

  • string: Returns the previous message

setRedirect

This method sets the object redirect options. This is only used if the redirect method is called. Setting $msg is optional; if not provided the value set internally by the controller will be used.

void setRedirect($url, [$msg = null], [$type = 'message'])

  • string $url: The redirect URL

  • string $msg: Optional message on redirect

  • string $type: Optional message type, default is 'message'

  • void: No return type

_addPath

This private method adds search paths for JModel and JView subclass files.

void _addPath($type, $path)

  • string $type: The path type (for example- model, view)

  • mixed $path: Directory or array of directories to add

  • void: No return type

_createFileName

This private method creates a filename based on $type and $parts; $type can be either 'view' or 'model'. $parts must contain the key 'name' and if $type is 'view' it can optionally contain the key 'type', which relates to the layout.

string _createFileName($type, $parts)

  • string $type: The resource type of the filename to create

  • mixed $parts: File name parts

  • string: Returns the parsed filename

_createModel

This private method returns a reference to a new JModel subclass object. $name is the class name suffix, normally the entity name. $prefix is the class name prefix, normally $controllerName.'Model'.

JModel &_createModel($name, [$prefix = ''], [$config = array()])

  • string $name: The name of the model

  • string $prefix: Optional model prefix

  • array $config: Optional configuration array for the model

  • object: Returns a reference to a JModel subclass object

_createView

This private method returns a reference to a new JView subclass object. $name is the class name suffix, normally the entity name. $prefix is the class name prefix, normally $controllerName.'View'. $type is the layout, normally HTML.

mixed &_createView($name, [$prefix = ''], [$type = ''], [$config = array()])

  • string $name: The name of the view

  • string $prefix: Optional view prefix

  • string $type: Optional view type

  • array $config: Optional configuration array for the view

  • mixed: Returns a reference to a JView subclass object

_setPath

This private method sets an entire array of search paths for JModel and JView subclass files.

void _addPath($type, $path)

  • string $type: The path type (for example- model, view)

  • mixed $path: The new set of search paths, can be a string or array

  • void: No return type