Accesing articles in Joomla 4 programmatically

156 views Asked by At

I try to get access via PHP to Joomla 4.3 articles using a standalone script. In the following code I get a error in this line $mvcFactory = \Joomla\CMS\Factory::getApplication()->bootComponent('com_content')->getMVCFactory();

I didn't get an error in the PHP log, just an 500 error from the Joomla framework: Sorry, there was a problem we could not recover from. The server returned a "500 - Whoops, looks like something went wrong."

My code:

    use Joomla\CMS\Factory;
    use Joomla\CMS\Component\ComponentHelper;
    use Joomla\CMS\Filter\OutputFilter;
    use Joomla\CMS\Router\Route;
    use Joomla\CMS\Table\Table;
    use Joomla\Component\Content\Administrator\Extension\ContentComponent;  
    use Joomla\Component\Content\Site\Helper\RouteHelper;
    use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

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

    $app = new Joomla\CMS\Application\SiteApplication();
    \Joomla\CMS\Factory::$application = $app;

    $app = Factory::getApplication();
    $mvcFactory = $app->bootComponent('com_content')->getMVCFactory();
    $articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);
    $table = $articleModel->getTable(); 

Does anyone have an idea?

Thanks Robin

My goal is to access articles in Joomla 4 programmatically using PHP. And I want to know why this code is not working:

    $mvcFactory = $app->bootComponent('com_content')->getMVCFactory();
    $articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);
    $table = $articleModel->getTable(); 
1

There are 1 answers

2
Alex On

Tested on Joomla 5

define('_JEXEC', 1);
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);

define('JPATH_BASE', dirname(__DIR__) . "/html");
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Language; 
use Joomla\CMS\Language\Text;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
// use Joomla\CMS\Table\Table;

// 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;

// The solution is to register extension namespaces, after setting the application as global app.
$app->createExtensionNamespaceMap();


$lang = Language::getInstance("en-GB");
$app->loadLanguage($lang);


$articleId = 16;


$factory = Factory::getApplication()->bootComponent('com_content')->getMVCFactory();
$model = $factory->createModel('Article')->getItem($articleId);
echo $model->introtext. "<br>"; // or fulltext
//echo "<pre style='color:black;'>"; print_r($model); echo "</pre>";