Reviewing Joomla! applications

While most people know Joomla as a content management system, Joomla is also a platform where any number of applications can sit on top. Let's take a look at three that are prepackaged with the CMS. So first go to the root of a fresh copy of a Joomla installation and open up index.php.


                      
  1. <?php
  2. /**
  3.  * @package Joomla.Site
  4.  * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  5.  * @license GNU General Public License version 2 or later; see LICENSE.txt
  6.  */
  7.  
  8. // Set flag that this is a parent file.
  9. define('_JEXEC', 1);
  10. define('DS', DIRECTORY_SEPARATOR);
  11.  
  12. if (file_exists(dirname(__FILE__) . '/defines.php')) {
  13. include_once dirname(__FILE__) . '/defines.php';
  14. }
  15.  
  16. if (!defined('_JDEFINES')) {
  17. define('JPATH_BASE', dirname(__FILE__));
  18. require_once JPATH_BASE.'/includes/defines.php';
  19. }
  20.  
  21. require_once JPATH_BASE.'/includes/framework.php';
  22.  
  23. // Mark afterLoad in the profiler.
  24. JDEBUG ? $_PROFILER->mark('afterLoad') : null;
  25.  
  26. // Instantiate the application.
  27. $app = JFactory::getApplication('site');
  28.  
  29. // Initialise the application.
  30. $app->initialise();
  31.  
  32. // Mark afterIntialise in the profiler.
  33. JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
  34.  
  35. // Route the application.
  36. $app->route();
  37.  
  38. // Mark afterRoute in the profiler.
  39. JDEBUG ? $_PROFILER->mark('afterRoute') : null;
  40.  
  41. // Dispatch the application.
  42. $app->dispatch();
  43.  
  44. // Mark afterDispatch in the profiler.
  45. JDEBUG ? $_PROFILER->mark('afterDispatch') : null;
  46.  
  47. // Render the application.
  48. $app->render();
  49.  
  50. // Mark afterRender in the profiler.
  51. JDEBUG ? $_PROFILER->mark('afterRender') : null;
  52.  
  53. // Return the response.
  54. echo $app;
  55.  

                      

So you will notice down here we are calling the getApplication function on JFactory and the site parameter is being passed in.


                      
  1. // Instantiate the application.
  2. $app = JFactory::getApplication('site');

                      

JFactory::getApplication loads the application in Joomla that is going to be run and the one for the front end is called site. So the one that runs from index. php at the root of the Joomla installation is the front end site application.

If we go back to administrator, there is another index.php file.


                      
  1. <?php
  2. /**
  3.  * @package Joomla.Administrator
  4.  * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  5.  * @license GNU General Public License version 2 or later; see LICENSE.txt
  6.  */
  7.  
  8. // Set flag that this is a parent file
  9. define('_JEXEC', 1);
  10. define('DS', DIRECTORY_SEPARATOR);
  11.  
  12. if (file_exists(dirname(__FILE__) . '/defines.php')) {
  13. include_once dirname(__FILE__) . '/defines.php';
  14. }
  15.  
  16. if (!defined('_JDEFINES')) {
  17. define('JPATH_BASE', dirname(__FILE__));
  18. require_once JPATH_BASE.'/includes/defines.php';
  19. }
  20.  
  21. require_once JPATH_BASE.'/includes/framework.php';
  22. require_once JPATH_BASE.'/includes/helper.php';
  23. require_once JPATH_BASE.'/includes/toolbar.php';
  24.  
  25. // Mark afterLoad in the profiler.
  26. JDEBUG ? $_PROFILER->mark('afterLoad') : null;
  27.  
  28. // Instantiate the application.
  29. $app = JFactory::getApplication('administrator');
  30.  
  31. // Initialise the application.
  32. $app->initialise(array(
  33. 'language' => $app->getUserState('application.lang')
  34. ));
  35.  
  36. // Mark afterIntialise in the profiler.
  37. JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
  38.  
  39. // Route the application.
  40. $app->route();
  41.  
  42. // Mark afterRoute in the profiler.
  43. JDEBUG ? $_PROFILER->mark('afterRoute') : null;
  44.  
  45. // Dispatch the application.
  46. $app->dispatch();
  47.  
  48. // Mark afterDispatch in the profiler.
  49. JDEBUG ? $_PROFILER->mark('afterDispatch') : null;
  50.  
  51. // Render the application.
  52. $app->render();
  53.  
  54. // Mark afterRender in the profiler.
  55. JDEBUG ? $_PROFILER->mark('afterRender') : null;
  56.  
  57. // Return the response.
  58. echo $app;
  59.  

                      

Down here again we are calling getApplication out of the JFactory class, but this time we are passing administrator in as the parameter.


                      
  1. // Instantiate the application.
  2. $app = JFactory::getApplication('administrator');

                      

So this way there is a completely separate backend from the front end.

Then finally if we go back up a level and then go to the installation folder, open up the index.php file there.


                      
  1. <?php
  2. /**
  3.  * @package Joomla.Installation
  4.  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  5.  * @license GNU General Public License version 2 or later; see LICENSE.txt
  6.  */
  7.  
  8. // PHP 5 check
  9. if (version_compare(PHP_VERSION, '5.2.4', '<')) {
  10. die('Your host needs to use PHP 5.2.4 or higher to run this version of Joomla!');
  11. }
  12.  
  13. /**
  14.  * Constant that is checked in included files to prevent direct access.
  15.  */
  16. define('_JEXEC', 1);
  17.  
  18. /**
  19.  * Constant that defines the base path of the installed Joomla site.
  20.  */
  21. define('JPATH_BASE', dirname(__FILE__));
  22.  
  23. // Set path constants.
  24. $parts = explode(DIRECTORY_SEPARATOR, JPATH_BASE);
  25. array_pop($parts);
  26.  
  27. define('JPATH_ROOT', implode(DIRECTORY_SEPARATOR, $parts));
  28. define('JPATH_SITE', JPATH_ROOT);
  29. define('JPATH_CONFIGURATION', JPATH_ROOT);
  30. define('JPATH_ADMINISTRATOR', JPATH_ROOT . '/administrator');
  31. define('JPATH_LIBRARIES', JPATH_ROOT . '/libraries');
  32. define('JPATH_PLUGINS', JPATH_ROOT . '/plugins');
  33. define('JPATH_INSTALLATION', JPATH_ROOT . '/installation');
  34. define('JPATH_THEMES', JPATH_BASE);
  35. define('JPATH_CACHE', JPATH_ROOT . '/cache');
  36. define('JPATH_MANIFESTS', JPATH_ADMINISTRATOR . '/manifests');
  37.  
  38. /*
  39.  * Joomla system checks.
  40.  */
  41. error_reporting(E_ALL);
  42. @ini_set('magic_quotes_runtime', 0);
  43. @ini_set('zend.ze1_compatibility_mode', '0');
  44.  
  45. /*
  46.  * Check for existing configuration file.
  47.  */
  48. if (file_exists(JPATH_CONFIGURATION.'/configuration.php') &amp;&amp; (filesize(JPATH_CONFIGURATION.'/configuration.php') > 10) &amp;&amp; !file_exists(JPATH_INSTALLATION.'/index.php')) {
  49. header('Location: ../index.php');
  50. exit();
  51. }
  52.  
  53. //
  54. // Joomla system startup.
  55. //
  56.  
  57. // Bootstrap the Joomla Framework.
  58. require_once JPATH_LIBRARIES.'/import.php';
  59.  
  60. // Botstrap the CMS libraries.
  61. require_once JPATH_LIBRARIES.'/cms.php';
  62.  
  63. // Joomla library imports.
  64. jimport('joomla.database.table');
  65. jimport('joomla.environment.uri');
  66. jimport('joomla.utilities.arrayhelper');
  67.  
  68. // Create the application object.
  69. $app = JFactory::getApplication('installation');
  70.  
  71. // Initialise the application.
  72. $app->initialise();
  73.  
  74. // Render the document.
  75. $app->render();
  76.  
  77. // Return the response.
  78. echo $app;
  79.  

                      

If you scroll down all the way to the bottom you will notice there is yet one more call to getApplication.


                      
  1. // Create the application object.
  2. $app = JFactory::getApplication('installation');

                      

This time installation is being passed in. The installation application is an application that helps you can install the Joomla CMS for the first time, and then you completely remove the installation application once it's done. So after installation, the site and administrator applications are the ones you are going to work with the most. Whenever you're inside a PHP file that's running within Joomla you can always tell which application you're running.

To do this, bring in the application object and then call the isAdmin function on that object.


                      
  1. $obj = JFactory::getApplication();
  2. if($obj->isAdmin()){
  3. // process admin code or whatever related to backend of joomla
  4. }else{
  5. // process site code or whatever related to frontend of joomla
  6. }
  7.  

                      

So in this case, we are detecting whether or not we are we are running the administrator application and if we could either bail out of this code or do something else as opposed to if we were running the front end.

In addition to the front end and the backend applications it's also possible to start with the Joomla platform and build your own applications.Knowing the difference between the applications will help you run your code in the right place. It may also inspire you to release your own custom applications based solely on the Joomla platform.