3

Please advise on how to access the Joomla framework in 3.10 using an external php file in Joomla 3 which can work in Joomla 4 too

I am getting this issue:

Warning: Constant JPATH_BASE already defined in F:\wamp64\www\16december2022\includes\app.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0005  367960  {main}( )   ...\getmodel.php:0
2   0.0258  794208  require_once( 'F:\wamp64\www\16december2022\includes\app.php )  ...\getmodel.php:10
3   0.0263  794272  define( $constant_name = 'JPATH_BASE', $value = 'F:\\wamp64\\www\\16december2022' ) ...\app.php:21

( ! ) Warning: file_get_contents(F:\wamp64\www\16december2022/templates/system/build_incomplete.html): Failed to open stream: No such file or directory in F:\wamp64\www\16december2022\includes\app.php on line 27
Call Stack
#   Time    Memory  Function    Location
1   0.0005  367960  {main}( )   ...\getmodel.php:0
2   0.0258  794208  require_once( 'F:\wamp64\www\16december2022\includes\app.php )  ...\getmodel.php:10
3   0.0301  794560  file_get_contents( $filename = 'F:\\wamp64\\www\\16december2022/templates/system/build_incomplete.html' )   ...\app.php:27

Code of getmodel.php which is working when using JFactory, but when using Factory not working. I think JFactory is not recommended in Joomla 4x and onwards

<?php

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));  
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_BASE . '/includes/app.php';

use Joomla\CMS\Factory;
$mainframe = Factory::getApplication('site');
    
     // GET using JInput
    $jinput  = Factory::getApplication()->input;
    $ModelId = $jinput->get('s', '', 'INT');

    $db = JFactory::getDBO();   
    $query = $db->getQuery(true);

    $query-> function
?>

created app.php and modified last line as referred in https://issues.joomla.org/tracker/joomla-cms/34586 in includes folder

<?php

/**
 * @package    Joomla.Site
 *
 * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

// Saves the start time and memory usage.
$startTime = microtime(1);
$startMem  = memory_get_usage();

if (file_exists(dirname(__DIR__) . '/defines.php')) {
    include_once dirname(__DIR__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__DIR__));
    require_once JPATH_BASE . '/includes/defines.php';
}

// Check for presence of vendor dependencies not included in the git repository
if (!file_exists(JPATH_LIBRARIES . '/vendor/autoload.php') || !is_dir(JPATH_ROOT . '/media/vendor')) {
    echo file_get_contents(JPATH_ROOT . '/templates/system/build_incomplete.html');

    exit;
}

require_once JPATH_BASE . '/includes/framework.php';

// Set profiler start time and memory usage and mark afterLoad in the profiler.
JDEBUG && \Joomla\CMS\Profiler\Profiler::getInstance('Application')->setStart($startTime, $startMem)->mark('afterLoad');

// Boot the DI container
$container = \Joomla\CMS\Factory::getContainer();

/*
 * Alias the session service keys to the web session service as that is the primary session backend for this application
 *
 * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
 * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
 * deprecated to be removed when the class name alias is removed as well.
 */
$container->alias('session.web', 'session.web.site')
    ->alias('session', 'session.web.site')
    ->alias('JSession', 'session.web.site')
    ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

// Set the application as global app
\Joomla\CMS\Factory::$application = $app;

// Execute the application.
var_dump($app);

1 Answer 1

1
+50

It looks like you have error reporting turned on and only a warning is breaking things. Look at the error message - it's simply saying that the constant is already defined. You need to either turn down the error reporting config in PHP, or rewrite the code to only define JPATH_BASE if it's not already.

For example:

<?php

if (!defined('_JEXEC')) define('_JEXEC', 1);
if (!defined('JPATH_BASE')) define('JPATH_BASE', __DIR__);

require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

// Boot the DI container
$container = \Joomla\CMS\Factory::getContainer();

/*
 * Alias the session service keys to the web session service as that is the primary session backend for this application
 *
 * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
 * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
 * deprecated to be removed when the class name alias is removed as well.
 */
$container->alias('session.web', 'session.web.site')
    ->alias('session', 'session.web.site')
    ->alias('JSession', 'session.web.site')
    ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

// Set the application as global app
\Joomla\CMS\Factory::$application = $app;

var_dump($app);
2
  • Thanks, although error gone but still unable to use joomla framework in external file in 4x. trying with tweaks will let know soon
    – Ruchika
    Dec 27, 2022 at 15:56
  • thanks, able to work done by slight modifying
    – Ruchika
    Dec 29, 2022 at 5:15

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.