3

Recently there have been a number of questions posted on JSE relating to how to load the Framework that in most cases is really asking how to load up the Joomla CMS either from a command line or via a browser to run other processing either internally or externally.

Having attempted to answer a few of the questions myself and seen others also helping by posting up similar snippets of code I have noticed they still left the person originating the question having to fiddle or do more.

So I have spent the day trying to answer my own question, how to create an instance of Joomla?

1 Answer 1

4

A complete working example is below if you just want to cut and paste it.

There are four possible flavours of Joomla that can be created based on the applications of Site, Administrator, API and CLI and each has its own quirks which is why I believe the code that I and other have been posting up have not always worked as intended.

The two important distinctions between the four applications is the Site and Administrator will only work when they are called from a Browser, for example https://mysite/thisfile.php while the other two, API and CLI, are intended to work as command line tools, e.g. php thisfile.php. Each will fail if called from the wrong environment.

Which Joomla application is going to work best with your requirements is up to you but to my mind it will come down how are you going to call or create the instance, browser or via a program/terminal and do you require access to an extension(plugin or component) that is only available in one of these applications environments.

The location of where the file is placed in Joomla directory, or outside of the Joomla directory, is also important in making the previous examples posted work correctly. Calling one application class from the wrong directory can cause it to pick up the wrong set of defines.php and framework.php file and due to an array_pop() I found in some files makes a big difference to whether your sample of code will work.

My sample below is intended to work from the root directory of Joomla by default however it can be placed outside of Joomla or in a sub-folder of Joomla provided the the root folder of Joomla is specified in the $joomla_directory property.

And once all that is sorted, each application environment has slightly different requirements for setting up the Sessions correctly that I have tried to address by defining the values in various properties in each block relevant to each environment.

My code as posted below, supplied as is, has been created from comparing the three app.php files used for Site, Administrator and API and the Joomla.php used by CLI and stepping through code to build the correct Joomla environment.

To use the code you will see it is in two parts, the first part contains four blocks of code, one for each Joomla environment or Application that you can call, and the blocks set up the properties for the second part,framework and application object to be used. You only need to have one of these blocks, you can delete the other three to reduce the code size and make it easier to follow.

The second part of the code goes about establishing the instance along with the session values and readies the required $app for use.

Sample last updated 28/06/2023

    <?php
    /**
     * Use this code to create an instance of the Joomla CMS in one of the four available applications.
     *
     * Usage:
     * Uncomment a block of code in the Part 1 to suit the application you wish to use. Cooment or remove the other blocks
     *
     * The 'site' and 'admin' only work when called from a URL, i.e. Browser or cUrl. e.g. 'https:\\mysite/thisfile.php '
     *
     * For 'api' and 'cli' they both can be called from a comand line, e.g. '> thisfile.php'
     *
     */
    define('_JEXEC', 1);
    
//If this file is not placed in the /root directory of a Joomla instance put the directory for Joomla libraries here.
    $joomla_directory = null;
    
    /**---------------------------------------------------------------------------------
     * Part 1 - Load the Framework and set up up the environment properties
     * -------------------------------------------------------------------------------*/
    
    /**
     *  Site - Front end application when called from Browser via URL.
    */                                                  // Remove this '*/' to comment out this block
    define('JPATH_BASE', (isset($joomla_directory)) ? $joomla_directory : __DIR__ );
    require_once JPATH_BASE . '/includes/defines.php';
    require_once JPATH_BASE . '/includes/framework.php';
    $class_name             =  new \Joomla\CMS\Application\SiteApplication;
    $session_alias          = 'session.web';
    $session_suffix         = 'web.site';
    /** end Site config */
    
    /**
     *  Admin - Backend end application when called from Browser via URL.
    
    define('JPATH_BASE', (isset($joomla_directory)) ? $joomla_directory . '\administrator' : __DIR__ . '\administrator' );
    require_once JPATH_BASE . '/includes/defines.php';
    require_once JPATH_BASE . '/includes/framework.php';
    $class_name             =  new \Joomla\CMS\Application\AdministratorApplication;
    $session_alias          = 'session.web';
    $session_suffix         = 'web.administrator';
    /** end Admin config */
    
    /**
     *  Api - Api application when called from Browser via URL.
    
    define('JPATH_BASE', (isset($joomla_directory)) ? $joomla_directory . '\api' : __DIR__ . '\api' );
    require_once JPATH_BASE . '/includes/defines.php';
    require_once JPATH_BASE . '/includes/framework.php';
    $class_name             =  new \Joomla\Console\Application;
    $session_alias          = 'session';
    $session_suffix         = 'cli';
    /** end Api config */
    
    /**
     *  Cli - Cli application when called from Browser via URL.
    
    define('JPATH_BASE', (isset($joomla_directory)) ? $joomla_directory  : __DIR__ );
    require_once JPATH_BASE . '/includes/defines.php';
    require_once JPATH_BASE . '/includes/framework.php';
    $class_name             =  new \Joomla\Console\Application;
    $session_alias          = 'session';
    $session_suffix         = 'cli';
    /** end Cli config */
    
    /**---------------------------------------------------------------------------------
     * Part 2 - Start the application from the container ready to be used.
     * -------------------------------------------------------------------------------*/
    // Boot the DI container
    $container = \Joomla\CMS\Factory::getContainer();
    
    // Alias the session service key to the web session service.
    $container->alias($session_alias, 'session.' . $session_suffix)
                ->alias('JSession', 'session.' . $session_suffix)
                ->alias(\Joomla\CMS\Session\Session::class, 'session.' . $session_suffix)
                ->alias(\Joomla\Session\Session::class, 'session.' . $session_suffix)
                ->alias(\Joomla\Session\SessionInterface::class, 'session.' . $session_suffix);
    
    // Instantiate the application.
    $app = $container->get($class_name::class);
    // Set the application as global app
     \Joomla\CMS\Factory::$application = $app;
    
    
    /**---------------------------------------------------------------------------------
     * Insert your own code here.
     * -------------------------------------------------------------------------------*/
     //If output of the $app is displayed then you have a working environment to start your code.
    print_r($app);
1
  • Good overview of the subject, could be good part in the official docs too, I think.
    – Zollie
    Commented Jun 23, 2023 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.