Решено - Joomla Как вызвать контроллер из внешнего скрипта - PullRequest
0 голосов
/ 14 апреля 2020

Я пытаюсь запустить функции контроллера 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);

1 Ответ

0 голосов
/ 14 апреля 2020

Я не получаю ничего, чтобы создать экземпляр контроллера из любого другого места вместо компонента контроллера.

Поэтому я посещаю класс BaseController, в котором лежит метод getInstance(). Вы можете взломать это, установив task через приложение ввода.

<code><?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 "
";
...