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



我尝试从外部脚本启动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 JoomlaCMSFactory;
// 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);

我没有从任何其他地方而不是从控制器的组件实例化控制器。

因此,我访问了getInstance()方法所在的BaseController类。您可以通过应用程序输入设置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 JoomlaCMSFactory;
// 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>";

最新更新