I'm getting some errors when I try to load J4 framework in an external file.

For Joomla! 4.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
HALVAM
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Aug 10, 2022 9:03 pm

I'm getting some errors when I try to load J4 framework in an external file.

Post by HALVAM » Wed Aug 10, 2022 10:31 pm

Greetings.

I'm starting to use Joomla 4 as the main CMS for my projects and I am doing some tests to see how much has it change from 3.x.

I'm currently having some issues with an external file that to loads J4 framework, but I just can't find a way that make it work like Joomla 3.

First, there isn't a lot of information on how to do it, the closest thing is this code from this GitHub page https://gist.github.com/PhilETaylor/0c3 ... 47c54cfb9b

But it doesn't work 100%, I'm getting errors when I try to use some methods or classes.

For example: I want to display a SEF URL using jRoute, for this scenario, I used the Articles component.

This is the code I'm using:

Code: Select all

<?php
define('_JEXEC', 1);
define('JPATH_BASE', '../');
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)

$url = JRoute::_("index.php?view=article&id=97&Itemid=255");

echo $url;

?>
This is the error that appears on screen.

Image

Another example is when I try to load a module using jHTML and "loadmodule" using \Joomla\CMS\HTML\HTMLHelper::_('content.prepare', '{loadmoduleid 1735}'); I get another error screen with a different stack trace.

Both errors don't appear in Joomla 3.

What do you think is causing this issue? Is there another way to load J4 framework?

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1120
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: I'm getting some errors when I try to load J4 framework in an external file.

Post by ceford » Thu Aug 11, 2022 5:25 am

In your provider file you need either a use statement:

use Joomla\Component\Content\Administrator\Helper\AssociationsHelper;

or the full path of the AssociationsHelper class. This does not appear to be related to the code you quoted.

Just curious: what is it you do that requires booting the framework from an external php file?

HALVAM
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Aug 10, 2022 9:03 pm

Re: I'm getting some errors when I try to load J4 framework in an external file.

Post by HALVAM » Thu Aug 11, 2022 11:24 pm

ceford wrote:
Thu Aug 11, 2022 5:25 am
In your provider file you need either a use statement:

use Joomla\Component\Content\Administrator\Helper\AssociationsHelper;

or the full path of the AssociationsHelper class. This does not appear to be related to the code you quoted.

Just curious: what is it you do that requires booting the framework from an external php file?
Its for a cultural institution, I redesign their website that was originally built with Joomla 3, now upgraded to J4. It has a custom search engine that was developed by other person.

This search engine isn’t implemented as a normal plugin or component, instead, it is located in an external php file that has the Joomla framework integrated into it, the results are loaded into the main website with JSON.

Update: I made some changes. Jroute works now, but HTMLhelper don't.

Code: Select all

<?php
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__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;

use Joomla\Component\Content\Administrator\Helper\AssociationsHelper;

//var_dump($app)

$app->createExtensionNamespaceMap();

$url = \Joomla\CMS\HTML\HTMLHelper::_('{zooitem: 14185 text: MYTEXT output: url}');
echo $url;

?>
This is what appears on screen.

Image

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1120
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: I'm getting some errors when I try to load J4 framework in an external file.

Post by ceford » Fri Aug 12, 2022 1:21 am

The stack trace is more useful - look down the call list until you find the one in your external code that calls HTMLHelper.php.

HALVAM
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Aug 10, 2022 9:03 pm

Re: I'm getting some errors when I try to load J4 framework in an external file.

Post by HALVAM » Mon Aug 15, 2022 8:53 pm

I had been making some changes to the code over the weekend, it seems that Joomla's core features and mos t of the plugins are working now. There are some other plugins that refuse to work, but they are not important for what I need.

Notherless, thank you Ceford so much for the help.


Post Reply

Return to “Joomla! 4.x Coding”