advance component development by azrul rahim

25
Azrul Rahim

Upload: john-coonen

Post on 08-May-2015

4.022 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Advance Component Development by Azrul Rahim

Azrul Rahim

Page 2: Advance Component Development by 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

Page 3: Advance Component Development by Azrul Rahim

Introduction to Joomla MVCBusiness logic in Joomla

ExamplesSome useful utilities and libraries

JFactory, JRequest, and others Introduction to some advance topics

Error handling Internationalization

Page 4: Advance Component Development by Azrul Rahim

All component is stored in /components/ folder

component/com_hello/ hello.php

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

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

Page 5: Advance Component Development by Azrul Rahim

JOOMLA 1.0 JOOMLA 1.5

Page 6: Advance Component Development by Azrul Rahim
Page 7: Advance Component Development by Azrul Rahim

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

Page 8: Advance Component Development by Azrul Rahim

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

Page 9: Advance Component Development by Azrul Rahim

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();

Page 10: Advance Component Development by Azrul Rahim

Process user requestDO NOT

Do db calls Echo anything!

Pass the execution to view

Page 11: Advance Component Development by Azrul Rahim

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

}

Page 12: Advance Component Development by Azrul Rahim

Render the outputGrab data from modelHave direct access to the default

model

Page 13: Advance Component Development by Azrul Rahim

Layout file does the final HTML output

default.php

nogreetings.php

Page 14: Advance Component Development by Azrul Rahim

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.

Page 15: Advance Component Development by Azrul Rahim

More on Joomla! API

Page 16: Advance Component Development by Azrul Rahim

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

Page 17: Advance Component Development by Azrul Rahim

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

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

Page 18: Advance Component Development by Azrul Rahim

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

Page 19: Advance Component Development by Azrul Rahim

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

Page 20: Advance Component Development by Azrul Rahim

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

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

Page 21: Advance Component Development by Azrul Rahim

DO NOT use fopen, fread …Use Joomla calls

JFolder JFile JPath

Page 22: Advance Component Development by Azrul Rahim

Display warning message

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

Page 23: Advance Component Development by Azrul Rahim

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

}

Page 24: Advance Component Development by Azrul Rahim
Page 25: Advance Component Development by Azrul Rahim

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