php - Symfony4.4:PUT 处理被重定向
问题描述
我已经从 Symfony 3.x 更新到 Symfony 4.4.16 并验证了操作。
当我按“注册/更改”保存表单时,我被重定向到表单输入屏幕。
我想要做的是将输入的信息保存在数据库中并显示顶部屏幕和成功消息。
你有什么改变吗?当我尝试php bin/console router:match /admin/initialSetting --method=PUT
时,它似乎没有问题。
控制器
/*
* @Method("GET")
* @Route("/initialSetting")
* @Template("@AppBundle/Security/initialSetting.html.twig")
*/
public function initialSettingAction(Request $request)
{
$key = $request->query->get("key");
$staff = $this->get("admin.staffService")->getStaffByUrlKey($key);
// Create form
$form = $this->createForm(InitialSettingType::class, $staff, array(
"method" => "PUT",
"action" => $this->generateUrl("app_security_initialsetting", array("key" => $key)),
));
// Show screen
return array(
'form' => $form->createView(),
);
}
/*
* @Method("PUT")
* @Route("/initialSetting")
* @Template("@AppBundle/Security/initialSetting.html.twig")
*/
public function updateInitialSettingAction(Request $request)
{
$key = $request->query->get("key");
$staff = $this->get("admin.staffService")->getStaffByUrlKey($key);
// Create Form
$form = $this->createForm(InitialSettingType::class, $staff, array(
"method" => "PUT",
"action" => $this->generateUrl("app_security_updateinitialsetting", array("key" => $key)),
));
// Request processing
if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) {
// URL key clear, save
$this->get("admin.staffService")->clearUrlKey($staff);
$this->get("admin.staffService")->save($staff);
$this->get('session')->getFlashBag()->add('success', 'Staff profile Initial settings have been completed.');
// Login
$this->autoLogin($staff);
return $this->redirect($this->generateUrl('app_admin_default_index'));
} else {
$this->get('session')->getFlashBag()->add('error', 'Staff profile Initial settings could not be completed. Please check the input contents.');
}
// Show screen
return array(
'form' => $form->createView(),
);
}
服务.yaml
app.form.type.initialSettingType:
class: AppBundle\Form\Type\Staff\InitialSettingType
arguments: [ "@service_container", "@admin.staff" ]
tags:
- { name: form.type }
admin.staff:
class: AppBundle\Model\Entity\Staff
初始设置.html.twig
<div class="formGroup">
{{ form_widget(form.submit, {label: "Registration/change", attr: {class: "btn"}}) }}
</div>
Postscript
查看浏览器开发工具中的代码,发现POST指定了表单的方法。
是否缺少软件包更新?
<form name="initialSetting" method="post" action="/url/url/" novalidate="novalidate" autocomplete="off">
<input type="hidden" name="_method" value="PUT">
版本
sensio/framework-extra-bundle v5.2.4
symfony/form v4.4.19
解决方案
我将其更改为下面的代码并解决了问题。
/**
*
* @Method("GET")
* @Route("/initialSetting")
*
* @Template("@AhiSpAdminBundle/Security/initialSetting.html.twig")
*/
public function initialSettingAction(Request $request)
{
return $this->initialSettingAndUpdateAction($request);
}
/**
*
* @Method("PUT")
* @Route("/initialSetting")
*
* @Template("@AhiSpAdminBundle/Security/initialSetting.html.twig")
*/
public function updateAction(Request $request)
{
return $this->initialSettingAndUpdateAction($request);
}
public function initialSettingAndUpdateAction(Request $request)
{
$key = $request->query->get("key");
$staff = $this->get("admin.staffService")->getStaffByUrlKey($key);
if (!$staff || $staff->getPassword()) {
throw new HttpException(
400,
The specified stuff prowl initialization URL does not exist or has expired.
"\n".
"Ask the administrator to reissue the URL."
);
}
$form = $this->createForm(InitialSettingType::class, $staff, array(
"method" => "PUT",
"action" => $this->generateUrl("ahi_sp_admin_security_update", array("key" => $key)),
));
if ($request->isMethod('PUT')) {
if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) {
$this->get("admin.staffService")->clearUrlKey($staff);
$this->get("admin.staffService")->save($staff);
$this->get('session')->getFlashBag()->add('success', 'Staff profile Initial settings have been completed.');
$this->autoLogin($staff);
return $this->redirect($this->generateUrl('ahi_sp_admin_default_index'));
} else {
$this->get('session')->getFlashBag()->add('error', 'Staff profile Initial settings could not be completed. Please check the input contents.');
}
}
return array(
'form' => $form->createView(),
);
}
推荐阅读
- oracle - 保存在表格中的最后一条记录超过一列
- angular - Angular 8 - Chartist - 不在表格中加载图表
- sql - 查询获取祖父母和孙子
- pycharm - AttributeError:部分初始化的模块“boto3”没有属性“资源”(很可能是由于循环导入)
- excel - 如何禁用或限制拆分工作表窗格中的滚动
- google-app-engine - 在 Google App Engine 上安装 Torch 时遇到问题
- css - 当同一容器中的另一个 div 具有未定义的高度时,设置容器内 div 的大小
- asterisk - 在被叫方使用 Asterisk 中的 ARI 接听电话后 x 秒后挂断电话
- javascript - 将一串赞成计数转换为 JavaScript 中的数字数组的函数
- java - 将kafka-consumer设置为consumer-group中的容灾消费者?