Core Php Component Presentation


Published on

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

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Core Php Component Presentation

  1. 1. Building Components for J!1.5 As Presented by Steven Pignataro Owner of ‘corePHP’ Web Development
  2. 2. What do we build for? <ul><li>Joomla! 1.5 is built to run on PHP4 and PHP5 </li></ul><ul><li>Need to keep this in mind when developing extensions. </li></ul><ul><li>Most developers are dropping support for PHP4 – some are not – you make the choice. </li></ul>
  3. 3. Development Applications <ul><li>‘ corePHP’ Developers use Aptana and extended plugins </li></ul><ul><ul><li> </li></ul></ul><ul><li>J!Dump – great tool to get a detailed out put for debugging. </li></ul><ul><ul><li> </li></ul></ul>
  4. 4. Where do we start? <ul><li>Joomla! 1.5 has 2 locations that we are going to be developing for: </li></ul><ul><ul><li>Joomla! Frontend (public accessible) </li></ul></ul><ul><ul><li>Joomla! Backend (admin accessible) </li></ul></ul>
  5. 5. Libraries <ul><li>J!1.5 provides a extensive collection of useful libraries that are easily called via jimport: </li></ul><ul><ul><li>Jimport(‘joomla.mail.*’); </li></ul></ul><ul><ul><li>Jimport(‘joomla.filesystem.file’); </li></ul></ul><ul><li>Open up libraries folder and call by the main folder / sub folder / filename. </li></ul><ul><li>Can be called like this: </li></ul><ul><ul><li>Jimport(‘cache.cache’); </li></ul></ul>
  6. 6. How to grab from Request <ul><li>Development with PHP Applications require the use of grabbing data from $_GET, $_GET, $_FILES, $_COOKIE, $_REQUEST, etc. </li></ul><ul><li>Joomla! 1.0.x we used mosGetParam(); we now pull from the JRequest function which gives us additional control: </li></ul><ul><ul><li>JRequest::setVar() </li></ul></ul><ul><ul><li>JRequest::getVar() </li></ul></ul>
  7. 7. JRequest::GetVar() <ul><li>How do we use it? </li></ul><ul><ul><li>$value = $JRequest::getVar(‘value’, 0); </li></ul></ul><ul><li>Other Methods for getting values: </li></ul><ul><ul><li>getInt() </li></ul></ul><ul><ul><li>getFloat() </li></ul></ul><ul><ul><li>getBool() </li></ul></ul><ul><ul><li>getWord() </li></ul></ul><ul><ul><li>getCmd() </li></ul></ul><ul><ul><li>getString() </li></ul></ul><ul><li>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. </li></ul>
  8. 8. JRequest::setVar() <ul><li>JReqeust::setVar() methods allows you to set the values into the request. </li></ul><ul><li>Examples of use are: </li></ul><ul><ul><li>JRequest::setVar(‘task’, ‘displayData’); </li></ul></ul>
  9. 9. Multilingual Support <ul><li>Joomla! allows superior support for UTF-9 (Unicode Transformation Format-8) ecnodting. Available strings are used through JText: </li></ul><ul><ul><li>_() </li></ul></ul><ul><ul><ul><li>echo JText::_(‘ARTICLE_COUNT’); </li></ul></ul></ul><ul><ul><li>sprintf() -> equivalent to PHP sprintf() </li></ul></ul><ul><ul><ul><li>$value = JText::sprintf(‘ARTICLE # NOT FOUND’, 92); </li></ul></ul></ul><ul><ul><ul><li>Language INI file will have: </li></ul></ul></ul><ul><ul><ul><ul><li>ARTICLE # NOT FOUND=Article #%d not found </li></ul></ul></ul></ul><ul><ul><li>printf() -> equivalent to PHP printf() </li></ul></ul>
  10. 10. Database <ul><li>The core of Joomla! has two database drivers: </li></ul><ul><ul><li>MySQL </li></ul></ul><ul><ul><li>MySQLi </li></ul></ul><ul><li>The database can be globalized using JFactory calls: </li></ul><ul><ul><li>$db =& JFactory::getDBO(); </li></ul></ul>
  11. 11. How to Query the Database <ul><li>We use setQuery() method to create the object we want to execute: </li></ul><ul><ul><li>$db =& Jfactory::getDBO(); </li></ul></ul><ul><ul><li>$query = ‘SELECT * FROM ‘ </li></ul></ul><ul><ul><li>. $db->nameQuote(‘#__corephp’) </li></ul></ul><ul><ul><li>. ‘ WHERE ‘ </li></ul></ul><ul><ul><li>. $db->nameQuote(‘firstname’) </li></ul></ul><ul><ul><li>. ‘ = ‘ </li></ul></ul><ul><ul><li>. $db->Quote(‘Steven’) </li></ul></ul><ul><ul><li>$db->setQuery($query) </li></ul></ul>
  12. 12. How to receive results <ul><li>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. </li></ul>
  13. 13. Database Methods <ul><li>loadResult(): string </li></ul><ul><li>loadResultArray( numinarray): array Array { [0] => Foo [1] => Bar } </li></ul>
  14. 14. Database Methods (cont.) <ul><li>loadAssoc(): array Array { [id] => 25 [name] => Steven } </li></ul><ul><li>loadAssocList( key: string=‘’): array Array { [0] => Array { [id] => 90 [name] => Steven } [1] => Array { [id] => 91 [name] => Michael } } </li></ul>
  15. 15. Database Methods (cont.) <ul><li>loadObject(): stdClass stdClass Object { [id] => 25 [name] => Steven } </li></ul><ul><li>loadObjectList( key: string=‘’): array Array { [0] => stdClass Object { [id] => 90 [name] => Steven } [1] => stdClass Object { [id] => 91 [name] => Michael } } </li></ul>
  16. 16. Database Methods <ul><li>loadRow(): array Array { [0] => Foo [1] => Bar } </li></ul><ul><li>loadRowList( key: int): array (Example: $db->loadRowList(0)); Array { [0] => Array { [id] => 90 [name] => Steven } [1] => Array { [id] => 91 [name] => Michael } } </li></ul>
  17. 17. JRoute <ul><li>Joomla! allows us to take advantage of SEF URI’s – in order to get these we need to use the JRoute::_() method </li></ul>
  18. 18. How do we use JRoute? <ul><li>With JRoute you will wrap your links like the following: </li></ul><ul><ul><li>Echo JRoute::_(‘index.php?option=com_penguinpower&cat=1&data=6’); </li></ul></ul><ul><li>With SEF Turned on the above link would possible produce something as follows: </li></ul><ul><ul><li> </li></ul></ul><ul><li>Not entirely SEF friendly </li></ul>
  19. 19. Building the Route <ul><li>To build the route – you need to create a file called router.php in the root of the component folder. </li></ul><ul><li>Two functions will be abvailable in this file: </li></ul><ul><ul><li>BuildRoute() </li></ul></ul><ul><ul><li>ParseRoute() </li></ul></ul><ul><ul><li>Prefixed prior to above functions is the component name. </li></ul></ul>
  20. 20. Example of router.php <ul><li>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; } </li></ul>
  21. 21. Example Cont. <ul><li>Function pengiunpowerParseRoute($segments) { $query = array(); if (isset($segments[0])) { $query[‘cat’] = $segments[0]; if (isset($segments[1])) { $query[‘data’] = $segments[1]; } } return $query; } </li></ul>
  22. 22. Router.php – the truth <ul><li>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. </li></ul><ul><li>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. </li></ul>
  23. 23. Errors <ul><li>J! 1.5 provides superior error messaging over the previous versions of Joomla! by using the class methods: </li></ul><ul><ul><li>JError::raiseError() </li></ul></ul><ul><ul><li>JError::raiseWarning() </li></ul></ul><ul><ul><li>JError::raiseNotice() </li></ul></ul>
  24. 24. raiseError <ul><li>JError::raiseError(‘403’, JText::_(‘You are not a penguin – go back north’)); </li></ul><ul><li>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. </li></ul>
  25. 25. raiseWarning <ul><li>JError::raiseWarning(‘ERROR_CODE’, JText::_(‘What makes a penguin fly during a cold night?’)); </li></ul><ul><li>This outputs simple messages above the mainBody of the site. </li></ul>
  26. 26. ACL <ul><li>Sorry – this is only an hour long – please request further information about ACL and how your site can be improved with ‘corePHP’ Community ACL. </li></ul>
  27. 27. Security <ul><li>Remember to start all your files with the following statement (this is one step to security – but do not stop there): </li></ul><ul><ul><li>Defined(‘_JEXEC’) or die(‘Penguin Bites’); </li></ul></ul>
  28. 28. SQL Injection <ul><li>To ensure your code is secure use the following methods to protect your data: </li></ul><ul><ul><li>$db =& JFactory::getDBO(); </li></ul></ul><ul><ul><li>$data = $db->QuotegetEscaped(JRequest(‘data’)); </li></ul></ul>
  29. 29. Extension Access Control <ul><li>There is a simple and easy way to allow access to copmonents and this can be done by doing the following example: </li></ul><ul><ul><li>$acl =& JFactory::getACL(); $acl ->_mos_add_acl(‘com_penguinpower’, ‘manage’, ‘users’, ‘super administrator’); </li></ul></ul>
  30. 30. Redirects <ul><li>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 </li></ul>
  31. 31. Redirects in use <ul><li>$mainframe->direct(‘index.php?option=com_penguinpower’); or </li></ul><ul><li>$mainframe->redirect(‘index.php?option=com_the-zend-penguin’, ‘Why fear the Zend Penguin?’); </li></ul>
  32. 32. What's next? <ul><li>Well one there is a massive API – no way it can be covered in one sitting. Recommended reading: </li></ul><ul><li>Mastering Joomla! 1.5 Extension and Framework Development by James Kennard </li></ul>