在使用CakePHP提交表单提交时传递URL变量的问题FormHelper
问题描述:
我正在编写我的第一个CakePHP应用程序,并且刚刚编写密码重置表单的第二部分,其中用户收到包含该网站链接的电子邮件,以及何时点击它,他们被要求输入并确认一个新的密码。在使用CakePHP提交表单提交时传递URL变量的问题FormHelper
的页面的URL是这样的:
/users/reset_password_confirm/23f9a5d7d1a2c952c01afacbefaba41a26062b17
的视图是这样的:
<?php echo $form->create('User', array('action' => 'reset_password_confirm')); ?>
<?php
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->hidden('static_hash');
?>
<?php echo $form->end('Reset password'); ?>
然而这产生一个形式,如:
<form id="UserResetPasswordConfirmForm" method="post" action="https://*.com/users/reset_password_confirm/8">
的问题是用户标识(本例中为8)将被添加到表单操作中。这不是一个真正的问题在这里,但我想通过哈希传递给我的控制器:
function reset_password_confirm($static_hash=null) {
// function body
}
$static_hash
现在填充了8,而不是从URL哈希值。
我知道我可以通过自己创建表单标签而不是使用$form->create
来解决这个问题,但是有没有更好的方法来做到这一点?
答
$form->create('User', array('action' => '…', 'id' => false));
就明确设置PARAMS你不希望传递给null
或false
。不幸的是,这是一个蛋糕为了自己的利益而过于聪明的例子。 ; O)
你也许还可以做这样的事情发布到再次在同一网址:
$form->create('User', $this->here);
答
怎么样把它当作一个参数,而不是形式的数据:
<?php
echo $form->create('User', array('action' => 'reset_password_confirm', $static_hash));
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->end('Reset password');
?>
和控制器:
function reset_password_confirm($static_hash = null) {
// Check if form is submitted
if (!empty($this->data)) {
// if it submitted then do your logic
} else {
$this->set('static_hash', $static_hash); // Else, pass the hash to the view, so it can be passed again when form is submitted
}
}
希望这有助于:)