Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Joomla 4 framework from external php file #34586

Closed
cloudfaction opened this issue Jun 21, 2021 · 6 comments
Closed

Access Joomla 4 framework from external php file #34586

cloudfaction opened this issue Jun 21, 2021 · 6 comments

Comments

@cloudfaction
Copy link

cloudfaction commented Jun 21, 2021

Steps to reproduce the issue

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');

Expected result

framework loaded, able to use it

Actual result

framework not loaded

System information (as much as possible)

J4 RC3

Additional comments

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.

@dgrammatiko
Copy link
Contributor

Check the include directory, you need to load one more file:

// 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';
require_once JPATH_BASE . '/includes/app.php';

@cloudfaction
Copy link
Author

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 comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34586.

@PhilETaylor

This comment was marked as abuse.

@cloudfaction
Copy link
Author

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.

@mavrosxristoforos
Copy link

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.

@mavrosxristoforos
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants