如何在Joomla插件和组件之间共享代码?

如何在Joomla插件和组件之间共享代码?

问题描述:

我正在写一个Joomla插件,用于访问存储在自写组件中的数据。如何在Joomla插件和组件之间共享代码?

如何访问该组件的代码?我对表格和模型特别感兴趣。

有没有官方的做法呢?

获取模型非常简单。只需在插件代码中包含来自组件的模型PHP文件并根据需要创建对象。

最好处理模型中的所有表操作,但是有方法可以在插件本身中加载表。

这里是你如何从加载模型插件:

<?php 

// Path to component 
$componentPath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'mycomponent'; 

// Include model 
require_once $componentPath . DS . 'models' . DS . 'example.php'; 

// You need to specify table_path because by default model uses 
// JPATH_COMPONENT_ADMINISTRATOR . DS . 'tables' 
// and you will not have correct JPATH_COMPONENT_ADMINISTRATOR in the plu-in 
// unless you specify it in config array and pass it to constructor 
$config = array(
    'table_path' => $componentPath . DS . 'tables' 
); 

// Create instance 
$model = new MycomponentModelExample($config); 

?> 

这里是你如何从插件加载表:

<?php 

// 1. Add the path so getInstance know where to find the table 
$tablePath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'mycomponent' . DS . 'tables'; 
JTable::addIncludePath($tablePath); 

// 2. Create instance of the table 
$tbl = JTable::getInstance('tableName', 'Table'); 

?>