学习完helloworld组件的前端代码后,现在开始学习后端的代码,后端代码的目录结构和前端类似:
helloworld.php是组件的后端入口文件,和前端入口文件主要的不同是通过以下代码:
// Set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-helloworld {background-image: url(../media/com_helloworld/images/tux-16x16.png);}');
设置了自定义的图标,并通过以下代码:
// require helper file
JLoader::register('HelloWorldHelper', JPATH_COMPONENT . '/helpers/helloworld.php');
引入了helper文件。helper文件中定义了addSubmenu--子菜单设置和getActions--获取用户可用操作的方法。
controller.php主控制器和前端一样。access.xml和config.xml分别定义了后端的ACL(权限控制)的属性和配置属性。controllers目录下是组件的自控制器,helloworld.php和helloworlds.php分别控制helloworld和helloworlds视图。helloworld.php重写了父类JControllerForm的allowAdd和allowEdit方法,helloworlds.php重写了父类JControllerForm的getModel方法。
language目录下是语言配置信息,定义了组件代码中用到的字符宏。
models\helloworlds.php为helloworlds视图提供数据,代码中定义了getListQuery();方法构造获取数据列表并排序的Query语句。models\helloworld.php为helloworld视图提供数据,提供了getTable--获取Table对象、getForm--获取表单、getScript--获取脚本、loadFormData--获取表单数据和canDelete--是否可以删除数据。
models\fields\helloworld.php定义了getOptions()方法获取数据库数据,并与前端对应。
models\forms目录下包含的xml文件定义了一些forms用到的字段属性,包括哪些字段需要验证。helloworld.js文件为验证器增加了一个句柄,每次greeting字段修改后都要验证。
models\rules\greeting.php提供服务端验证功能,代码中定义了继承JFormRule的JFormRuleGreeting类,这里只声明了一个正则表达式用于测试;详细功能请参考joomla源码中的libraries/joomla/form /rule.php文件。
sql目录下是组件用到的数据库文件,相关sql语句的理解请参见sql教程链接。
table\helloworld.php定义了继承JTable的HelloWorldTableHelloWorld类,重载了bind和load方法用于处理后端的配置参数和ACL参数,以及asset相关方法:
- _getAssetName(): 获取asset的默认名
- _getAssetTitle(): 获取可识别的asset名
- _getAssetParentId(): 数据库中asset父ID
views\helloworlds\view.html.php负责helloworlds视图显示,其中addToolBar()方法负责添加Toolbar,可在该方法中设置要使用的joomla风格Toolbar按钮或自定义按钮;setDocument()方法负责设置Document属性。下面是display方法的说明:
function display($tpl = null)
{
// 获取application
$app = JFactory::getApplication();
$context = "helloworld.list.admin.helloworld";
//从模块取数据
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->state = $this->get('State');
$this->filter_order = $app->getUserStateFromRequest($context.'filter_order', 'filter_order', 'greeting', 'cmd');
$this->filter_order_Dir = $app->getUserStateFromRequest($context.'filter_order_Dir', 'filter_order_Dir', 'asc', 'cmd');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
//确认用户权限
$this->canDo = HelloWorldHelper::getActions();
//错误判断
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// 设置子菜单
HelloWorldHelper::addSubmenu('helloworlds');
//设置Toolbar的items
$this->addToolBar();
// 显示模板
parent::display($tpl);
// 设置document
$this->setDocument();
}
views\helloworlds\tmpl\defalt.php文件设计了view显示的样式,相关html说明请查阅
html教程。
views\helloworld\view.html.php负责helloworld视图显示,其内容与views\helloworlds\view.html.php类似。
views\helloworld\submitbutton.js提供了响应save操作的验证功能js代码,该js的路径在views\helloworld\view.html.php中有设置。
views\helloworlds\tmpl\edit.php为helloworld\view.html.php提供模板,其代码说明如下:
JHtml::_('behavior.formvalidation');//告诉joomla使用js进行验证
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
method="post" name="adminForm" id="adminForm" class="form-validate"><!--//加入CSS类form-validate -->
<div class="form-horizontal">
<?php foreach ($this->form->getFieldsets() as $name => $fieldset): ?>
<fieldset class="adminform">
<legend><?php echo JText::_($fieldset->label); ?>
<div class="row-fluid">
<div class="span6">
<?php foreach ($this->form->getFieldset($name) as $field): ?>
<div class="control-group"></legend><!--//显示表单数据 -->
<div class="control-label"><?php echo $field->label; ?></div>
<div class="controls"><?php echo $field->input; ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
</fieldset>
<?php endforeach; ?>
</div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</form>
以上就是helloworld组件主要文件说明,以下是相关参考资料:
HTML、SQL、正则表达式资料
Helloworld英文文档
快速创建组件