我仍在学习如何创建自己的 Prestashop 1.7 控制器。我从以下位置复制代码:
class MyLittleController extends AdminController
{
public $bootstrap = true ;
public function __construct()
{
$this->table = 'my_sql_tab';
$this->className = 'MyLittle';
parent::__construct();
$this->addRowAction('view');
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->allow_export = true;
$this->_defaultOrderBy = 'fields_name';
$this->_defaultOrderWay = 'DESC';
$this->bulk_actions = array(
'delete' => array(
'text' => $this->trans('Delete selected', array(), 'Admin.Actions'),
'icon' => 'icon-trash',
'confirm' => $this->trans('Supprimer ?', array(), 'Admin.Notifications.Warning')
)
);
$this->fields_list = array(
'fields_name' => array('title' => $this->trans('fields_name', array(), 'Admin.Global'), 'align' => 'center', 'class' => 'fixed-width-xs'),
'fields_name2' => array('title' => $this->trans('fields_name2', array(), 'Admin.Global')),
'fields_name3' => array('title' => $this->trans('fields_name3', array(), 'Admin.Global'))
);
}
public function initPageHeaderToolbar()
{
if (empty($this->display)) {
$this->page_header_toolbar_btn['new_MyLittle'] = array(
'href' => self::$currentIndex.'&add&token='.$this->token,
'desc' => $this->trans('Add', array(), 'Admin.Catalog.Feature'),
'icon' => 'process-icon-new'
);
}
parent::initPageHeaderToolbar();
}
public function displayForm()
{
// Get default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('test'),
'name' => $this->name,
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value[$this->name] = Configuration::get($this->name);
return $helper->generateForm($fields_form);
}
public function setMedia()
{
parent::setMedia();
$this->addJqueryUi('ui.widget');
$this->addJqueryPlugin('tagify');
}
此代码的灵感来自 prestashop 1.7 文档和一些现有模块。它工作得很好,显示我的 SQL 表的内容。显示“添加”按钮,但是当我单击它时重定向到“?controller=Mylittle&add”,但内容仍然相同,它没有像其他控制器那样显示我的 renderForm。我错过了什么 ?
编辑:经过几次搜索后我忘记了:
$this->actions = array('add', 'view', 'edit', 'delete');
因此,当调用添加或编辑时,会显示我的“renderForm()”。当我调用“view”时,我的“rederview()”也显示得很好。希望它会帮助一些prestashop初学者。