在J3.x升级到J4.x后,有很多的API出现了不兼容的问题。这直接就导致有一些J3的插件在升级完成后直接崩溃的问题。下面文章主要记录常用的一下类和方法不兼容时的处理。
常见的错误信息及解决方案
1,$app->isAdmin() is not defined.
出现这个问题,是因为在Joomla4中,将isAdmin()方法从JApplication类中移除中。替代的代码如下:
$app= JFactory::getApplication(); $app->isClient("administrator"); //等价于之前的$app->isAdmin()
2,插件的触发代码发生了变化。
在J3x我们触发插件使用的代码如下:
// Process the content plugins. JPluginHelper::importPlugin('content'); $dispatcher = JEventDispatcher::getInstance(); $dispatcher->trigger('onContentPrepare', array ('com_content.article', &$item, &$item->params, $offset));
在J4x中需要修改如下:
// Process the content plugins. JPluginHelper::importPlugin('content'); $dispatcher = JFactory::getApplication(); $dispatcher->triggerEvent('onContentPrepare', array ('com_content.article', &$item, &$item->params, $offset));
3,Joomla4 Class 'Registry' not found
在使用前加上use use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper;
评论 (0)