开发者

How can I share code between Joomla plug-ins and components?

I'm writing a Joomla plug-in that access开发者_开发百科es the data stored in a self-written component.

How can I access the code of that component? I'm especially interested in the tables and models.

Is there an official way for doing that?


Getting the model is pretty easy. Just include the model PHP file from the component in the plug-in code and create your objects as needed.

It is better to handle all table manipulation in the model, but there are ways to load table in the plug-in itself.

Here is how you load the model from the plug-in:

<?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);

?>

Here is how you load the table from the plug-in:

<?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');

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜