32
Building Components for J!1.5 As Presented by Steven Pignataro Owner of ‘corePHP’ Web Development

Core Php Component Presentation

Embed Size (px)

DESCRIPTION

JoomlaEXPO presentation by Steve Pignataro, of CorePHP, on Components - May 16, 2008.

Citation preview

Page 1: Core Php Component Presentation

Building Components for J!1.5

As Presented by Steven Pignataro Owner of ‘corePHP’ Web

Development

Page 2: Core Php Component Presentation

What do we build for?

• Joomla! 1.5 is built to run on PHP4 and PHP5

• Need to keep this in mind when developing extensions.

• Most developers are dropping support for PHP4 – some are not – you make the choice.

Page 3: Core Php Component Presentation

Development Applications

• ‘corePHP’ Developers use Aptana and extended plugins– http://www.aptana.org

• J!Dump – great tool to get a detailed out put for debugging.– http://www.joomlacode.org/gf/project/jdump

Page 4: Core Php Component Presentation

Where do we start?

• Joomla! 1.5 has 2 locations that we are going to be developing for:– Joomla! Frontend (public accessible)– Joomla! Backend (admin accessible)

Page 5: Core Php Component Presentation

Libraries

• J!1.5 provides a extensive collection of useful libraries that are easily called via jimport:– Jimport(‘joomla.mail.*’);– Jimport(‘joomla.filesystem.file’);

• Open up libraries folder and call by the main folder / sub folder / filename.

• Can be called like this:– Jimport(‘cache.cache’);

Page 6: Core Php Component Presentation

How to grab from Request

• Development with PHP Applications require the use of grabbing data from $_GET, $_GET, $_FILES, $_COOKIE, $_REQUEST, etc.

• Joomla! 1.0.x we used mosGetParam(); we now pull from the JRequest function which gives us additional control:– JRequest::setVar()– JRequest::getVar()

Page 7: Core Php Component Presentation

JRequest::GetVar()

• How do we use it?– $value = $JRequest::getVar(‘value’, 0);

• Other Methods for getting values:– getInt()– getFloat()– getBool()– getWord()– getCmd()– getString()

• With these get strings we are guaranteeing that the values grabbed will be either an Integer or a String, or any of the other JRequest methods.

Page 8: Core Php Component Presentation

JRequest::setVar()

• JReqeust::setVar() methods allows you to set the values into the request.

• Examples of use are:– JRequest::setVar(‘task’, ‘displayData’);

Page 9: Core Php Component Presentation

Multilingual Support

• Joomla! allows superior support for UTF-9 (Unicode Transformation Format-8) ecnodting. Available strings are used through JText:– _()

• echo JText::_(‘ARTICLE_COUNT’);

– sprintf() -> equivalent to PHP sprintf()• $value = JText::sprintf(‘ARTICLE # NOT FOUND’, 92);• Language INI file will have:

– ARTICLE # NOT FOUND=Article #%d not found

– printf() -> equivalent to PHP printf()

Page 10: Core Php Component Presentation

Database

• The core of Joomla! has two database drivers:– MySQL– MySQLi

• The database can be globalized using JFactory calls:– $db =& JFactory::getDBO();

Page 11: Core Php Component Presentation

How to Query the Database

• We use setQuery() method to create the object we want to execute:$db =& Jfactory::getDBO();

$query = ‘SELECT * FROM ‘

. $db->nameQuote(‘#__corephp’)

. ‘ WHERE ‘

. $db->nameQuote(‘firstname’)

. ‘ = ‘

. $db->Quote(‘Steven’)

$db->setQuery($query)

Page 12: Core Php Component Presentation

How to receive results

• With Joomla! 1.5 we have similar methods that allow us to grab results from the database. In the next few slides I will briefly discuss each one.

Page 13: Core Php Component Presentation

Database Methods

• loadResult(): string

• loadResultArray( numinarray): arrayArray{

[0] => Foo[1] => Bar

}

Page 14: Core Php Component Presentation

Database Methods (cont.)• loadAssoc(): array

Array{

[id] => 25[name] => Steven

}• loadAssocList( key: string=‘’): array

Array{

[0] => Array{

[id] => 90[name] => Steven

}

[1] => Array{

[id] => 91[name] => Michael

}}

Page 15: Core Php Component Presentation

Database Methods (cont.)• loadObject(): stdClass

stdClass Object{

[id] => 25[name] => Steven

}• loadObjectList( key: string=‘’): array

Array{

[0] => stdClass Object{

[id] => 90[name] => Steven

}

[1] => stdClass Object{

[id] => 91[name] => Michael

}}

Page 16: Core Php Component Presentation

Database Methods• loadRow(): array

Array{

[0] => Foo[1] => Bar

}• loadRowList( key: int): array (Example: $db->loadRowList(0));

Array{

[0] => Array{

[id] => 90[name] => Steven

}

[1] => Array {[id] => 91[name] => Michael

}}

Page 17: Core Php Component Presentation

JRoute

• Joomla! allows us to take advantage of SEF URI’s – in order to get these we need to use the JRoute::_() method

Page 18: Core Php Component Presentation

How do we use JRoute?

• With JRoute you will wrap your links like the following:– Echo JRoute::_(‘index.php?

option=com_penguinpower&cat=1&data=6’);

• With SEF Turned on the above link would possible produce something as follows:– http://www.corephp.com/index.php/component/pengui

npower/1/6/

• Not entirely SEF friendly

Page 19: Core Php Component Presentation

Building the Route

• To build the route – you need to create a file called router.php in the root of the component folder.

• Two functions will be abvailable in this file:– BuildRoute()– ParseRoute()– Prefixed prior to above functions is the

component name.

Page 20: Core Php Component Presentation

Example of router.php

• function penguinpowerBuildRoute(&$query) {$segments = array();if (isset($query[‘cat’])) {

$segments[] = $query[‘cat’];unset($query[‘cat’];if(isset($query[‘data’])) {

$segments[] = $query[‘data’];unset($query[‘data’]);

}}

return $segments;}

Page 21: Core Php Component Presentation

Example Cont.

• Function pengiunpowerParseRoute($segments) {

$query = array();if (isset($segments[0])) {

$query[‘cat’] = $segments[0];if (isset($segments[1])) {

$query[‘data’] = $segments[1];}

}return $query;}

Page 22: Core Php Component Presentation

Router.php – the truth

• Some developers are skipping over this and not realizing the benfit this has to there component. By allowing your users to have high quality SEF links - you are allowing your users to have superior power.

• It is important to allow your users to have proper SEF links. JRoute gives this ability and is not difficult to use if you are familiar with the previous methods of SEFing a link.

Page 23: Core Php Component Presentation

Errors

• J! 1.5 provides superior error messaging over the previous versions of Joomla! by using the class methods:– JError::raiseError()– JError::raiseWarning()– JError::raiseNotice()

Page 24: Core Php Component Presentation

raiseError

• JError::raiseError(‘403’, JText::_(‘You are not a penguin – go back north’));

• Thins will provide a page with an error message with a 403 message. You can also perform other forms of errors such as 500 when an error occurs.

Page 25: Core Php Component Presentation

raiseWarning

• JError::raiseWarning(‘ERROR_CODE’, JText::_(‘What makes a penguin fly during a cold night?’));

• This outputs simple messages above the mainBody of the site.

Page 26: Core Php Component Presentation

ACL

• Sorry – this is only an hour long – please request further information about ACL and how your site can be improved with ‘corePHP’ Community ACL.

Page 27: Core Php Component Presentation

Security

• Remember to start all your files with the following statement (this is one step to security – but do not stop there):– Defined(‘_JEXEC’) or die(‘Penguin Bites’);

Page 28: Core Php Component Presentation

SQL Injection

• To ensure your code is secure use the following methods to protect your data:– $db =& JFactory::getDBO();– $data = $db-

>QuotegetEscaped(JRequest(‘data’));

Page 29: Core Php Component Presentation

Extension Access Control

• There is a simple and easy way to allow access to copmonents and this can be done by doing the following example:– $acl =& JFactory::getACL();

$acl ->_mos_add_acl(‘com_penguinpower’, ‘manage’, ‘users’, ‘super administrator’);

Page 30: Core Php Component Presentation

Redirects

• Using redirects could be used when saving data / copying items / creating a new item / publishing/unpublishing or anything that you might need to redirect the user to a different location

Page 31: Core Php Component Presentation

Redirects in use

• $mainframe->direct(‘index.php?option=com_penguinpower’);or

• $mainframe->redirect(‘index.php?option=com_the-zend-penguin’, ‘Why fear the Zend Penguin?’);

Page 32: Core Php Component Presentation

What's next?

• Well one there is a massive API – no way it can be covered in one sitting. Recommended reading:

• Mastering Joomla! 1.5 Extension and Framework Developmentby James Kennard