我的基本控制器具有以下内容:
protected $session;
public function before()
{
parent::before();
$this->session = Session::instance();
}
我扩展了这个控制器,当用户尝试登录时,如果用户名/密码尚未填写,我会重定向用户:
if($this->request->method() == Request::POST)
{
$username = $this->request->post('username');
$password = $this->request->post('password');
if(strlen($username) == 0)
{
$this->session->set('error', 'Please enter a Username');
Request::current()->redirect('user/login');
}
if(strlen($password) == 0)
{
$this->session->set('error', 'Please enter a Password');
Request::current()->redirect('user/login');
}
}
$error = $this->session->get_once('error');
$view = View::factory('user/login');
$view->bind('title', $title);
$view->bind('error', $error);
echo $view->render();
然而,$ error参数在返回时始终为NULL。这是因为会话通过Base控制器重置了吗?我该怎么做才能避免这种情况?
答案 0 :(得分:0)
为什么你这么简单的事情复杂化?没有必要使用会话进行错误处理......
if($this->request->method() == Request::POST)
{
$username = $this->request->post('username');
$password = $this->request->post('password');
if(strlen($username) == 0)
{
$error = 'Please enter a Username';
}
if(strlen($password) == 0)
{
$error = 'Please enter a Password';
}
}
$view = View::factory('user/login')
->bind('title', $title)
->bind('error', $error);
echo $view->render();