web-dev-qa-db-ja.com

関数内でJDateを使用すると、Joomla 4でクラスが見つかりません

私の最後の質問 mod_latest_articlesモジュールを変更しようとし、myCustomFunctionをArticlesLatestHelper.phpに次のように追加しました:

/**
 * @package     Joomla.Site
 * @subpackage  mod_articles_latest
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Module\ArticlesLatest\Site\Helper;

defined('_JEXEC') or die;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Site\Model\ArticlesModel;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

\JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

/**
 * Helper for mod_articles_latest
 *
 * @since  1.6
 */
abstract class ArticlesLatestHelper
{
    /**
     * Retrieve a list of article
     *
     * @param   Registry       $params  The module parameters.
     * @param   ArticlesModel  $model   The model.
     *
     * @return  mixed
     *
     * @since   1.6
     */
    public static function getList(Registry $params, ArticlesModel $model)
    { // this is the built-in function ... }


    public static function myCustomFunction($created_date)
    {
        $date = new JDate($created_date);
        echo $date->format('l, d F Y H:i', false, false);
    }   

@Zollieの solution を使用してdefault.phpから関数を呼び出すことができますが、関数内でJDateを使用すると、このエラーメッセージが表示されます

エラー:クラス 'Joomla\Module\ArticlesLatest\Site\Helper\JDate'が見つかりません:クラス 'Joomla\Module\ArticlesLatest\Site\Helper\JDate'が見つかりません

誰かが私に言うことができます、私は今ここで何が間違っているのですか?ありがとう!

2
webchun

それは明らかにJoomla 4のコアバグではなく、開発者のコ​​ーディングの間違いであり、問​​題はJoomla 3でも同じになるので、私は質問に答えています。

コードでJoomlaコアクラスを使用する場合は、そのクラスをファイルの先頭に含める必要があります。そうしないと、Joomlaは何を使用する必要があるのか​​わかりません。

_use Joomla\CMS\Date\Date;_

次に、コードで使用できます。

$date = new Date($created_date);

これらのクラスは、ポイントしない限り、Joomla全体で自動的に利用できるわけではありません。したがって、使用するクラスをファイルの先頭に含めます。

3
Zollie