How Joomla! Execution Flow Works

When any single request is made to Joomla it always loads and executes in the same pattern. Once you know how this pattern works you can make the right decisions on how to design your extensions.So all requests in Joomla either go to index.php at the root, or they go to the index.php file in the administrator folder.

In this case, we're going to take a look at the index.php file at the root.

Root of Joomla

  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 first Joomla loads what's known as the bootstrap code.

BootStrap Code for joomla

This code is going to load the configuration file and get the database started and do a few other things to get everything in place, so that Joomla can execute the rest of the code.

Then comes the application which is initialized.

Joomla frontend application initilialization

In this case, we're getting the site application and we're just getting it started so that we can begin to run code on the front end of Joomla

Next the application is routed.

Joomla application being routed

The router is going to take a look at the URL that's being passed into Joomla and decide what code it needs to run after that.

After the application is routed then Joomla dispatches the application.

Joomla Displatches application

In this step Joomla is running the component and all the modules. So it's going to run the login module and the menu modules and it's going to run the component that's going to generate the article that's going to be displayed. And it's going to gather all the output from all these different pieces and just hold them in memory for a little bit.

Render Joomla

Then finally, after it has all the output from all the different components and modules Joomla is going to load up the template and then backfill all of the placeholders for the module positions and the component. So now right before the output goes back to the browser we have one last chance to make any modifications we want. And then finally the output from the application is sent back to the browser.

output joomla

Even if you want to build something simple with no template, still go through index.php rather than build your own PHP script. For security it's important that all PHP requests go through Joomla and not directly to individual scripts.Joomla's standard execution flow gives you ample opportunity for adding code at strategic points. When you understand how Joomla executes you can avoid needless hacks and workarounds.