Loading the Joomla! Platform in an external script

If you are looking to execute an external PHP script, and still have access to the functionality of the platform and your site, you can do something like this in order to load the platform.

<?php define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
 
if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}
 
if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}
 
require_once JPATH_BASE.'/includes/framework.php';
 
/* Your code here */

If we take a closer look at the code, the first if construct will check if there exists a defines.php file in the directory of your external PHP script, and use if in case it does. If that file is not found, it will look in the includes subdirectory for defines.php. After the defines are included, the platform is loaded. There is really not much more to it, now you will be able to use the Joomla! platform like you would inside Jooml!. For example by instantiating JDatabase through $database = JFactory::getDBO(); or accessing the request through JRequest.

Removing the components administrator menu

So, your shiny new frontend component is all written and works flawlessly without any kind of bugs whatsoever and now you would like to remove the component menu item in the administrator Components menu. Why you say ? Of course because your shiny new component is a frontend-only component and you do not want to implement a dummy backend interface just because it is cool.

So here is how you do it, if you need to know how to create an installer script for your component, please refer to the official Joomla! MVC Component tutorial.

class Com_HelloworldInstallerScript {
    function postflight($type, $parent){
        if ($type == 'install' || $type == 'update') {
            $db = JFactory::getDBO();
            $query = $db->getQuery(true);
            $query->delete('#__menu');
            $id = JComponentHelper::getComponent('com_helloworld')->id;
            $query->where('component_id = '.$id);
            $query->where('client_id = 1');
            $db->setQuery($query);
            $db->query();
        }   
    }   
}

Note that during uninstall of a component which has had its menu entry removing in this manner, there will be a warning/error in versions 1.7.0 and older. The component will still uninstall like it should so do not worry if you see this error.