advance component development by azrul rahim

Post on 08-May-2015

4.023 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Advance Component Development by Azrul Rahim presentation given at CMS Expo in Denver, December 2008.

TRANSCRIPT

Azrul Rahim

Developer of JomComment & MyBlog JomSocial (www.jomsocial.com)

3 years of Joomla! coding experienceStarted with Joomla! 1.0 and now

code exclusively for Joomla! 1.5Slides co-written by Toby Patterson

Introduction to Joomla MVCBusiness logic in Joomla

ExamplesSome useful utilities and libraries

JFactory, JRequest, and others Introduction to some advance topics

Error handling Internationalization

All component is stored in /components/ folder

component/com_hello/ hello.php

// no direct accessdefined('_JEXEC') or die('Restricted access');

echo '<h1>Hello</h1>';

JOOMLA 1.0 JOOMLA 1.5

Model – manage data and logic. All database calls should be here

View – render the data from model. No business logic here please

Controller – control application flow Interpret user request Trigger appropriate model Pass model to view

http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,tutorials:components/

Minimal code here. Execute the controller

// Require the base controllerrequire_once (JPATH_COMPONENT.DS.'controller.php');

// Require specific controller if requestedif($controller = JRequest::getVar('controller')) {

require_once (JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php');}

// Create the controller$classname = 'HelloController'.$controller;$controller = new $classname( );

// Perform the Request task$controller->execute( JRequest::getVar('task'));

// Redirect if set by the controller$controller->redirect();

Process user requestDO NOT

Do db calls Echo anything!

Pass the execution to view

Extends JModelYour method represent business

logicclass HelloModelHello extends JModel{

/** * Gets the greeting * @return string The greeting to be displayed to the user */function getGreeting(){

$db =& JFactory::getDBO();

$query = 'SELECT greeting FROM #__hello';$db->setQuery( $query );$greeting = $db->loadResult();

return $greeting;}

}

Render the outputGrab data from modelHave direct access to the default

model

Layout file does the final HTML output

default.php

nogreetings.php

Separate application control, data logic and view

The important concepts to take away are: Controllers represent control logic; Models represent business/data logic; Views represent presentation logic; Layouts are for markup language.

More on Joomla! API

MVC – JController, JModel , JView JRequest – grab user

POST/GET/REQUEST data JText

multi-language support Use JText::_ ( ) and JText::sprintf( )

JFactory - access global objects JUser – User object

Implement “Factory” design pattern ::getApplication() (instead of global

$mainframe) ::getDBO(); ::getDocument();

Don’t just use JRequest::getVar( ….) ::getWord(…) ::getCmd(…) ::getString(…) ::getBool(…) ::getFloat(…) ::getInt(…)

A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.

A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.

From php.net, definition of a string

Ensure UTF-8 integrity Use JString:: functions JString::strlen(…)

Support internationalization JText::_(…) JText::sprintf(…)

DO NOT use fopen, fread …Use Joomla calls

JFolder JFile JPath

Display warning message

$mainframe = JFactory::getApplication();$mainframe->enqueuMessage(‘Warning’);

We want to fail fast! Do not let error propagate to other

part of the code

if($db->getErrorNum()){JError::raiseError( 500, $db->stderr());

}

if(condition_not_met()){JError::raiseError( 500, 'Syatem error');

}

http://developer.joomla.orghttp://docs.joomla.org/Developers

top related