我正在尝试模仿flashdata行为以显示一次性错误消息。
显示页面:
public function showLoginAndRegistrationPage()
{
$session = new Session();
$data['errors']['login'] = $session->getFormErrorFlashData('login', 'account');
$this->viewPresenter->display('basic', 'customer/login-registration', $data, 'Login/Register');
}
这是处理:
public function processLogin()
{
// Some code
if($this->formInputFilter->isValid()) {
// Some code
if($loginCredentials) {
// Some code
} else {
$errors = array(
'account' => 'Account does not exist.'
);
$session->setFormErrorFlashData('login', $errors);
header('Location: /login');
}
} else {
// Some code
}
}
在Session
类:
public function setFormErrorFlashData($form, $errors = array())
{
foreach($errors As $field => $message) {
$_SESSION['errors']["{$form}"]["{$field}"] = $message;
}
}
在Session
类中显示Flash数据:
public function getFormErrorFlashData($form, $field)
{
if(isset($_SESSION['errors']["{$form}"]["{$field}"])) {
$message = $_SESSION['errors']["{$form}"]["{$field}"];
unset($_SESSION['errors']["{$form}"]);
return $message;
}
}
$data['errors']['login']
中的showLoginAndRegistrationPage()
应在无效登录尝试后的视图中显示“帐户不存在”,但事实并非如此。我使用了var_dump()
,它总是NULL
。
在showLoginAndRegistrationPage()
中,$data['errors']['login']
总是空着。我已在else
中的processLogin()
块中签到,setFormErrorFlashData()
确实有效。我在设置检查后立即使用了var_dump()
,它确实返回了数据。只是在尝试无效后,我重定向到showLoginAndRegistrationPage()
之后我没有得到任何东西。
更新
public static function getFormErrorFlashData($form, $field)
{
var_dump($_SESSION);
var_dump($form, $field);
if(isset($_SESSION['errors']["{$form}"]["{$field}"])) {
$message = $_SESSION['errors']["{$form}"]["{$field}"];
unset($_SESSION['errors']["{$form}"]);
return $message;
}
}
结果:
// No attempts yet
array(1) { ["errors"]=> array(0) { } } string(5) "login" string(7) "account"
// After 1 invalid attempt
array(1) { ["errors"]=> array(0) { } } string(5) "login" string(7) "account"
// RESULT FROM ADDING:
// die(var_dump($session->getFormErrorFlashData('login','account')));
// after setFormErrorFlashData() inside the else block
array(1) { ["errors"]=> array(1) { ["login"]=> array(1) { ["account"]=> string(23) "Account does not exist." } } } string(5) "login" string(7) "account" string(23) "Account does not exist."