create php file to include joomla framework. works in J3
define('_JEXEC', 1);
// this file is in a subfolder 'scripts' under the main joomla folder
define('JPATH_BASE', realpath(dirname(FILE) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
// instantiate application
$app = JFactory::getApplication('site');
framework loaded, able to use it
framework not loaded
J4 RC3
I am aware of jfactory, to be changed to factory, and use PHP namespaces. but even then i cannot get the J4 framework loaded in external page.
sorry, not working
define('_JEXEC', 1);
define('JPATH_BASE', realpath(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');
This is not a bug.
This kind of question should be asked in the Support Forum or worse, in Joomla Stack Exchange.
Basically, take the /includes/app.php, copy it, and remove the last line, tweak it, and then you have your stack.... simple.
https://gist.github.com/PhilETaylor/0c36d87da0f2ad231b378b47c54cfb9b
<?php
define('_JEXEC', 1);
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);
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2021-06-21 15:16:47 |
Closed_By | ⇒ | cloudfaction |
Phil, you are probably right, but i have been seeking this answer and have not found it. Until now. Thank you for your reply. I will close this now.
Unfortunately something goes wrong with this method. While the application is loaded successfully, if you try to JRoute::_() something right below the code provided by @PhilETaylor, the site breaks.
$link = JRoute::_('index.php?option=com_content&view=article&id=1');
This causes a ClassNotFoundError
in [components/com_content/services/provider.php] (line 46)
The error message says:
Attempted to load class "AssociationsHelper" from namespace
"Joomla\Component\Content\Administrator\Helper".
Did you forget a "use" statement for another namespace?
If you call $app->execute();
before JRoute::_()
it works normally, but it loads the website, because... well obviously, the app is executed.
In my case, I need to load the framework in order to use ajax from within a module extension, without introducing a whole component, just to load a few things from the database. Any insight would be really appreciated.
Just found a joomla.stackexchange.com post that solves it. I didn't delete my comments, because I think this information is useful, as this issue appears very high on Google when someone searches to load Joomla 4 from an external php file.
The solution is to register extension namespaces, after setting the application as global app.
...
// Set the application as global app
\Joomla\CMS\Factory::$application = $app;
$app->createExtensionNamespaceMap();
Source: https://joomla.stackexchange.com/questions/32145/joomla-4-error-when-i-use-getarticleroute
Check the include directory, you need to load one more file: