我的 CakePHP 2.6 应用程序中有一个表单,其中有多个按钮可将表单提交到不同的操作。在 Cake 2.4 中,这是可行的,但在 2.6 上,提交到默认表单操作以外的任何内容都会导致黑洞 'auth'
错误。
就在表单结束之前,我有多个提交按钮,如下所示:
echo $this->Form->button('Default', array(
'type' => 'submit',
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'formaction' => '/posts/otheraction',
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'formaction' => '/posts/anotheraction',
));
Reading the docs,我看到‘auth’表示表单验证错误,或 Controller /操作不匹配错误。
。然而,这在过去是有效的——事情似乎变得更加严格了。如何让 Cake 接受其他操作提交的表单而不完全关闭安全性?
最佳答案
这似乎是不可能的(至少在没有跳过巨大的障碍的情况下不可能),因为该操作被无条件地合并到 token 中。
$hashParts = array(
$this->_lastAction, // <<<<<<<<<<<
serialize($fields),
$unlocked,
Configure::read('Security.salt')
);
$fields = Security::hash(implode('', $hashParts), 'sha1');
<强> https://github.com/cakephp/cakephp/blob/2.7.1/lib/Cake/View/Helper/FormHelper.php#L589
禁用此行为还可能会削弱安全性,因为发布到不打算使用数据的操作可能会导致意外行为。
您可以通过单个操作来处理此问题,例如,为各个提交按钮提供名称和值,然后在 Controller 操作中评估该值并执行需要完成的任何操作
echo $this->Form->button('Default', array(
'type' => 'submit',
'name' => 'action',
'value' => 'default'
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate1'
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate2'
));
switch ($this->request->data('action')) {
case 'default':
// ...
break;
case 'alternate1':
// ...
break;
case 'alternate2':
// ...
break;
}
关于php - 在 CakePHP 2.6 中向不同操作提交表单会导致黑洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31661266/