How to load the Joomla framework inside an external file


// Get Joomla! framework 
define( '_JEXEC', 1 ); 
define( '_VALID_MOS', 1 ); 
define( 'JPATH_BASE', realpath(dirname(__FILE__))); 
define( 'DS', DIRECTORY_SEPARATOR ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); 
$mainframe =& JFactory::getApplication('site'); 
$mainframe->initialise(); 

In the above code we assume that the external php file is located at the website root.

If the external file is located inside a folder in your Joomla installation then you must change this line accordingly so that it reflects the new file path:


define( 'JPATH_BASE', realpath(dirname(__FILE__)));

For example if your file is located inside the folder modules/mod_yourmodule/tmpl/ then the above line should be:


define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));

or if the file is located inside the folder components/com_yourcomponent/templates/default/ then the above line should be:


define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../../..' ));

Subscribe to our Blog
Subscribe to this content and receive updates directly in your inbox.
Name
Email

This website uses cookies so that we can provide you with the best user experience. By clicking "continue" you agree to cookies being used in accordance with our Cookies Policy.

Search