Joomla 如何从外部脚本调用控制器

Joomla How to call controller from external script

我尝试从外部脚本启动 joomla 控制器的功能。脚本的开头工作得很好,但控制器的使用却不行。 谢谢你的帮助,

<?php

define('_JEXEC', 1);    

// this file is in a subfolder 'cron' 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';

use Joomla\CMS\Factory;

// instantiate application
$app = Factory::getApplication('site');

// database connection
$db = Factory::getDbo();

jimport('joomla.application.component.controller');
JLoader::import('Article', JPATH_ROOT . '/components/com_content/controllers');
$controller = JControllerLegacy::getInstance('Article');
var_dump($controller);

除了控制器的组件之外,我无法从任何其他地方实例化控制器。

所以我访问了 BaseController class getInstance() 方法所在的位置。您可以通过应用程序输入设置 task 来解决这个问题。

<?php

define('_JEXEC', 1);

// This file is in a subfolder 'cron' 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';

use Joomla\CMS\Factory;

// Instantiate application
$app = Factory::getApplication('site');

// Get the input instance
$input = $app->input;

// Database connection
$db = Factory::getDbo();

/**
 * You have to provide a task for getting the controller instance.
 * The `article` is the controller filename as well as the controller class's suffix
 * See the controller class name is `ContentControllerArticle`
 *
 */
$input->set('task', 'article.');

/**
 * The first argument of the getInstance() function is the type.
 * The type is the component name here.
 * And you have to pass the base_path of the component through the $config argument.
 */
$controller = JControllerLegacy::getInstance('Content', ['base_path' => JPATH_ROOT . '/components/com_content']);

echo "<pre>";
print_r($controller);
echo "</pre>";