Symfony4.4:PUT 处理被重定向
Symfony4.4: PUT processing is redirected
我已经从 Symfony 3.x 更新到 Symfony 4.4.16 并验证了操作。
当我按“Registration/change”保存表单时,我被重定向到表单输入屏幕。
我想要做的是将输入的信息保存在数据库中并显示顶部屏幕和成功消息。
你有什么改变吗?
我试过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(),
);
}
services.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
initialSetting.html.twig
<div class="formGroup">
{{ form_widget(form.submit, {label: "Registration/change", attr: {class: "btn"}}) }}
</div>
后记
查看浏览器的开发工具中的代码,发现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(),
);
}
我已经从 Symfony 3.x 更新到 Symfony 4.4.16 并验证了操作。
当我按“Registration/change”保存表单时,我被重定向到表单输入屏幕。
我想要做的是将输入的信息保存在数据库中并显示顶部屏幕和成功消息。
你有什么改变吗?
我试过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(),
);
}
services.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
initialSetting.html.twig
<div class="formGroup">
{{ form_widget(form.submit, {label: "Registration/change", attr: {class: "btn"}}) }}
</div>
后记
查看浏览器的开发工具中的代码,发现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(),
);
}