Building Components for J!1.5 As Presented by Steven Pignataro Owner of ‘corePHP’ Web Development
May 08, 2015
Building Components for J!1.5
As Presented by Steven Pignataro Owner of ‘corePHP’ Web
Development
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.
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
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)
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’);
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()
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.
JRequest::setVar()
• JReqeust::setVar() methods allows you to set the values into the request.
• Examples of use are:– JRequest::setVar(‘task’, ‘displayData’);
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()
Database
• The core of Joomla! has two database drivers:– MySQL– MySQLi
• The database can be globalized using JFactory calls:– $db =& JFactory::getDBO();
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)
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.
Database Methods
• loadResult(): string
• loadResultArray( numinarray): arrayArray{
[0] => Foo[1] => Bar
}
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
}}
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
}}
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
}}
JRoute
• Joomla! allows us to take advantage of SEF URI’s – in order to get these we need to use the JRoute::_() method
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
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.
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;}
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;}
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.
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()
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.
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.
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.
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’);
SQL Injection
• To ensure your code is secure use the following methods to protect your data:– $db =& JFactory::getDBO();– $data = $db-
>QuotegetEscaped(JRequest(‘data’));
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’);
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
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?’);
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