Prestashop 1.6:如何使用管理控制器创建和提交自定义表单
Prestashop 1.6: How to create and submit custom form using admin controller
我一直在寻找关于这个问题的文档。没有找到想要的
我知道如何使用管理控制器加载管理模板。
我用
创建了一个表单
<form action="{$link->getModuleLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post">
这里 "pushnotification" 是我的模块名称,"AdminPushNotification" 是我的管理控制器名称。
当我点击提交时它会转到 http://example.com/en/module/pushnotification/AdminPushNotification URL 这不是有效的 URL 因此得到 404 页面
但我想提交表单并留在同一页面。
我不知道如何在 Admin Controller 中提交和处理表单提交。
提前致谢
我的模块文件结构:
推送通知
控制器
行政
AdminPushnotification.php
观点
模板
行政
pushnotificationform.tpl
我的管理控制器代码:
<?php
class AdminPushNotificationController extends ModuleAdminControllerCore
{
public function __construct() {
$this->bootstrap = true;
parent::__construct();
}
public function initContent()
{
parent::initContent();
$smarty = $this->context->smarty;
$this->context->smarty->assign(array(
'msg' => "",
'title' => ""
));
$content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$this->context->smarty->assign(array(
'content' => $this->content . $content
));
}
public function postProcess()
{
if (Tools::isSubmit('sendTokenMessage'))
{
$title = Tools::getValue('title');
$msg = Tools::getValue('message');
$content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$this->context->smarty->assign(array(
'content' => $this->content . $content,
'msg' => $msg,
'title' => $title
));
}
}
public function sendMessage($title,$msg)
{
$sql = " Select Token From Token";
$result = DB::getInstance()->execute($sql);
foreach($result as $row) {
$message = array("title" => $title, "text"=> $msg);
$message_status = send_notification($row["token"], $message);
}
return $message_status;
}
}
和我的 pushnotificationform.tpl 代码:
<div>
{if $msg!=""}
<p>{$msg}</p>
{/if}
</div>
<form action="{$link->getAdminLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post">
<div style="padding-bottom:10px;" class="form-group">
<label for="" class="control-label col-lg-6">Title</label>
<div class="col-lg-6">
<input class="form-control input-lg" type="text" name="title" />
</div>
</div>
<div class="form-group">
<label for="" class="control-label col-lg-6">Message</label>
<div class="col-lg-6">
<textarea name="message" class="form-control" style="min-width: 100%"></textarea>
</div>
</div>
<input type="submit" name="sendTokenMessage" value="send" class="btn btn-default" />
</form>
$link->getModuleLink()
将 link 检索到模块前端控制器。
您需要使用 $link->getAdminLink('AdminPushNotification')
创建一个 link 到您的管理控制器。
编辑:
您以错误的方式调用了 $link->getAdminLink()
方法。
您需要在表单中做的就是:
<form action="{$link->getAdminLink('AdminPushNotification')|escape:'htmlall':'utf-8'}" method="post">
getAdminLink()
方法与 getModuleLink()
不同,因为它只接受一个字符串参数,即您的管理模块控制器的名称 AdminPushNotification
.
您可以查看 classes/Link.php
class 了解各种 link 生成方法及其参数。
编辑2:
您分配了两次内容 - 在 postProcess()
和 initContent()
中抛出了错误。
使用 postProcess()
对输入进行一些后台工作,即。您的 send_message($title, $msg)
方法或验证。
使用initContent()
设置smartyvariables/templates等
所以我会像这样重构这两个方法:
protected $msg = "";
protected $title = "";
public function postProcess()
{
if (Tools::isSubmit('sendTokenMessage'))
{
// store inputs to object properties,
// here they can be validated and reused in initContent()
$this->title = Tools::getValue('title');
$this->msg = Tools::getValue('message');
// perhaps do some inputs validation here
$this->send_message($this->title, $this->msg);
}
}
public function initContent()
{
parent::initContent();
$smarty = $this->context->smarty;
// msg and title variables must be assigned before fetching template
// otherwise they are not recognized in template
$smarty->assign(array(
'msg' => $this->msg,
'title' => $this->title
));
$content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$smarty->assign(array(
'content' => $this->content . $content
));
}
当然还要记住在模板中转义 $msg
和 $title
以防止 XSS 并清理字符串以防止 SQL 注入。
我一直在寻找关于这个问题的文档。没有找到想要的
我知道如何使用管理控制器加载管理模板。 我用
创建了一个表单<form action="{$link->getModuleLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post">
这里 "pushnotification" 是我的模块名称,"AdminPushNotification" 是我的管理控制器名称。
当我点击提交时它会转到 http://example.com/en/module/pushnotification/AdminPushNotification URL 这不是有效的 URL 因此得到 404 页面
但我想提交表单并留在同一页面。
我不知道如何在 Admin Controller 中提交和处理表单提交。
提前致谢
我的模块文件结构: 推送通知 控制器 行政 AdminPushnotification.php 观点 模板 行政 pushnotificationform.tpl
我的管理控制器代码:
<?php
class AdminPushNotificationController extends ModuleAdminControllerCore
{
public function __construct() {
$this->bootstrap = true;
parent::__construct();
}
public function initContent()
{
parent::initContent();
$smarty = $this->context->smarty;
$this->context->smarty->assign(array(
'msg' => "",
'title' => ""
));
$content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$this->context->smarty->assign(array(
'content' => $this->content . $content
));
}
public function postProcess()
{
if (Tools::isSubmit('sendTokenMessage'))
{
$title = Tools::getValue('title');
$msg = Tools::getValue('message');
$content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$this->context->smarty->assign(array(
'content' => $this->content . $content,
'msg' => $msg,
'title' => $title
));
}
}
public function sendMessage($title,$msg)
{
$sql = " Select Token From Token";
$result = DB::getInstance()->execute($sql);
foreach($result as $row) {
$message = array("title" => $title, "text"=> $msg);
$message_status = send_notification($row["token"], $message);
}
return $message_status;
}
}
和我的 pushnotificationform.tpl 代码:
<div>
{if $msg!=""}
<p>{$msg}</p>
{/if}
</div>
<form action="{$link->getAdminLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post">
<div style="padding-bottom:10px;" class="form-group">
<label for="" class="control-label col-lg-6">Title</label>
<div class="col-lg-6">
<input class="form-control input-lg" type="text" name="title" />
</div>
</div>
<div class="form-group">
<label for="" class="control-label col-lg-6">Message</label>
<div class="col-lg-6">
<textarea name="message" class="form-control" style="min-width: 100%"></textarea>
</div>
</div>
<input type="submit" name="sendTokenMessage" value="send" class="btn btn-default" />
</form>
$link->getModuleLink()
将 link 检索到模块前端控制器。
您需要使用 $link->getAdminLink('AdminPushNotification')
创建一个 link 到您的管理控制器。
编辑:
您以错误的方式调用了 $link->getAdminLink()
方法。
您需要在表单中做的就是:
<form action="{$link->getAdminLink('AdminPushNotification')|escape:'htmlall':'utf-8'}" method="post">
getAdminLink()
方法与 getModuleLink()
不同,因为它只接受一个字符串参数,即您的管理模块控制器的名称 AdminPushNotification
.
您可以查看 classes/Link.php
class 了解各种 link 生成方法及其参数。
编辑2:
您分配了两次内容 - 在 postProcess()
和 initContent()
中抛出了错误。
使用 postProcess()
对输入进行一些后台工作,即。您的 send_message($title, $msg)
方法或验证。
使用initContent()
设置smartyvariables/templates等
所以我会像这样重构这两个方法:
protected $msg = "";
protected $title = "";
public function postProcess()
{
if (Tools::isSubmit('sendTokenMessage'))
{
// store inputs to object properties,
// here they can be validated and reused in initContent()
$this->title = Tools::getValue('title');
$this->msg = Tools::getValue('message');
// perhaps do some inputs validation here
$this->send_message($this->title, $this->msg);
}
}
public function initContent()
{
parent::initContent();
$smarty = $this->context->smarty;
// msg and title variables must be assigned before fetching template
// otherwise they are not recognized in template
$smarty->assign(array(
'msg' => $this->msg,
'title' => $this->title
));
$content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl');
$smarty->assign(array(
'content' => $this->content . $content
));
}
当然还要记住在模板中转义 $msg
和 $title
以防止 XSS 并清理字符串以防止 SQL 注入。