Got a question?  Ask a developer in our Developer's Corner Forum

Back To The Developer's Corner Main Page

Joomla! Execution Path Walkthrough

When debugging issues or doing development in Joomla!, it is helpful to understand the basic Joomla! Execution Path.  The execution path is the list of function calls that are made during each page request.  While the calls will change from request to request (depending on query string parameters, cookies, session information, etc), the basic path is the same each time.  We will cover the basics and provide information to help developers find information for their specific situation.

 

Almost all requests to Joomla! start with the index.php file found in the root of the Joomla! installation.  Administrator requests go to the .../administrator/index.php file, but are similar to front-end calls.  There are also some cases where certain pages can be called directly, but in most cases, index.php is the starting point.

 

.../index.php

The following code is a security measure and defines a variable other pages use to make sure no one calls them directly.  All other php files that should not be called directly in Joomla! should include a check for this variable at the top of the file:

// Set flag that this is a parent file
define( '_JEXEC', 1 );

We then setup some other variables and include/require some files.  The PROFILER code is used if you have Debug System turned On in the Global Configuration and is used to print out debug information at the bottom of the page.  We will see more calls like this throughout the file and will ignore them:

define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;

Finally, we do some work and get the Joomla! JApplication object and call it's initialise method:

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

The online Joomla! API site is a great place to use as a reference for looking up functions.  The getApplication function and initialise method can be found here:

http://api.joomla.org/Joomla-Framework/JFactory.html#getApplication

http://api.joomla.org/Joomla-Framework/Application/JApplication.html#initialise

 

The API lists the files associated with these two classes which can be found here:

.../libraries/joomla/factory.php

.../libraries/joomla/application/application.php

 

For now, we won't worry about the specifics of the JApplication initialise method.  If you want to open the file and look at the code to see what it does, that is the best way to learn.  For now, just know that it initialises the application. ;-)

 

Next, we import the system plugins and trigger the onAfterInitialise method on them:

JPluginHelper::importPlugin('system');
// trigger the onAfterInitialise events
JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
$mainframe->triggerEvent('onAfterInitialise');

The triggerEvent method is another one that we will see frequently but won't explain every time.  This call tells Joomla! to go through each enabled plugin of a certain type and call the event on it.  In this case, this is calling the System plugin onAfterInitialise event.

 

Next, we call the route method:

$mainframe->route();

This is the description of what the route method does and is in the application.php code:

/**
* Route the application.
*
* Routing is the process of examining the request environment to determine which
* component should receive the request. The component optional parameters
* are then set in the request object to be processed when the application is being
* dispatched.
*
* @abstract
* @access	public
*/
function route()

 

Then we authorize that the user is allowed to see this menu item:

// authorization
$Itemid = JRequest::getInt( 'Itemid');
$mainframe->authorize($Itemid);

Once we know the user is allowed, we dispatch the request to the appropriate component:

$option = JRequest::getCmd('option');
$mainframe->dispatch($option);

The dispatch method is a very important call.  This is the entry point into the component that is being requested.  Every request to Joomla! ends up going to a specific component and follows a similar path.  For this example, let's assume we are requesting the following URL:

/index.php?option=com_content&view=article&id=39&Itemid=37

 

This is telling Joomla! to route the request to the com_content component.  The Itemid query string variable tells Joomla! which menu item this request is for (as used above when authorizing).  Most of the other query string variables are used in the component, but a lot of them (view, controller, task, etc) are used across most components.

 

Joomla! will then execute the following file for the component:

.../components/com_content/content.php

For a component named com_somethingelse, Joomla! would execute:

 

.../components/com_somethingelse/somethingelse.php

 

 

To see a full explanation of the execution path once inside a component, see the article Joomla! Component Execution Path Walkthrough.

 

Finally, we render out the resulting response to the user:

$mainframe->render();
// trigger the onAfterRender events
JDEBUG ? $_PROFILER->mark('afterRender') : null;
$mainframe->triggerEvent('onAfterRender');
/**
 * RETURN THE RESPONSE
 */
echo JResponse::toString($mainframe->getCfg('gzip'));

The render method is another very important call.  This call will tell the JDocument object to render itself and will result in the template and modules being rendered.  This topic will also be covered in another future article that will eventually be linked to from here.

 

And that is it.  Not terribly complicated and easy to follow.

 

 

Other resources

PHP has wonderfully helpful functions called debug_backtrace and debug_print_backtrace that will tell you the path PHP took to get to the code you are at:

http://php.net/manual/en/function.debug-backtrace.php

If you know you are getting to a certain place in the code but not sure what path was taken, use these functions to help debug the issue.

 

PHP also has a wonderful function called print_r.  While the print function can be used to print a string or number, it can't be used to easily print out an object.  That is where print_r can be helpful:

http://php.net/manual/en/function.print-r.php

 

 

Part of the SourceCoast Network:

SourceCoast / Cooking Allergy Free / CovertApps

© 2009 CMS Market. All rights reserved.

GTranslate Joomla Module