谈到 Joomla,我是个业余爱好者,因为我上周才开始使用这个框架进行开发。截至目前,我正在阅读官方的 Joomla 教程,可以在他们的官方 Wiki 中找到。但是要么我做错了什么,要么我忘记了一些东西,或者说教程中没有提到。
我经历的最后一步是开发访问控制列表;但是,维护按钮未显示。
这是我到目前为止的代码:
管理员/视图/helloworld/view.html.php
class HelloWorldViewHelloWorld extends JView {
protected $form;
protected $item;
protected $script;
protected $canDo;
public function display($tpl = NULL){
$this->form = $this->get('Form');
$this->item = $this->get('Item');
$this->script = $this->get('Script');
$this->canDo = HelloWorldHelper::getActions($this->item->id);
if(count($errors = $this->get('Errors'))){
JError::raiseError(500, implode('<br />', $errors));
return false;
}
$this->addToolBar();
parent::display($tpl);
$this->setDocument();
}
protected function addToolBar(){
$input = JFactory::getApplication()->input;
$input->set('hidemainmenu', true);
$isNew = ($this->item->id == 0);
JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld');
if($isNew){
if($this->canDo->get('core.create')){
JToolBarHelper::apply('helloworld.apply', 'JTOOLBAR_APPLY');
JToolBarHelper::save('helloworld.save', 'JTOOLBAR_SAVE');
JToolBarHelper::custom('helloworld.save2copy', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false);
}
JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CANCEL');
} else {
if($this->canDo->get('core.edit')){
JToolBarHelper::apply('helloworld.apply', 'JTOOLBAR_APPLY');
JToolBarHelper::save('helloworld.save', 'JTOOLBAR_SAVE');
if($this->canDo->get('core.create')){
JToolBarHelper::custom('helloworld.save2copy', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false);
}
}
if($this->canDo->get('core.create')){
JToolBarHelper::custom('helloworld.save2copy', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false);
}
JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CLOSE');
}
}
protected function setDocument(){
$isNew = ($this->item->id < 1);
$document = JFactory::getDocument();
$document->setTitle($isNew ? JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING') : JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
$document->addScript(JURI::root() . $this->script);
$document->addScript(JURI::root() . "/administrator/components/com_helloworld/views/helloworld/submitbutton.js");
JText::script('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE');
}
}
admin/helpers/helloworld.php(部分)
abstract class HelloWorldHelper {
[...]
public static function getActions($messageId = 0){
jimport('joomla.access.access');
$user = JFactory::getUser();
$result = new JObject;
if(empty($messageId)){
$assetName = 'com_helloworld';
} else {
$assetName = 'com_helloworld.message.' . (int) $messageId;
}
$actions = JAccess::getActions('com_helloworld', 'component');
foreach($actions as $action){
$result->set($action->name, $user->authorise($action->name, $assetName));
}
return $result;
}
}
我试图调试这个使用var_dump($this->canDo)
,但我没有得到任何回应。我可能错过了什么?
更新: 在 views/HelloWorlds/view.html.php 中返回var_dump
:$this->canDo
object(JObject)#43 (1) { ["_errors":protected]=> array(0) { } }
这是在views/HelloWorlds/view.html.php中对上述函数的调用:
function display($tpl = NULL){
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->canDo = HelloWorldHelper::getActions();
if(count($errors = $this->get('Errors'))){
JError::raiseError(500, implode('<br />', $errors));
return false;
}
$this->addToolBar($this->pagination->total);
parent::display($tpl);
$this->setDocument();
}