Session Kohana 3:设置/获取flash消息和会话

Session Kohana 3:设置/获取flash消息和会话,session,kohana,kohana-3,Session,Kohana,Kohana 3,我的基本控制器具有以下功能: protected $session; public function before() { parent::before(); $this->session = Session::instance(); } 我扩展此控制器,当用户尝试登录时,如果未填写用户名/密码,我将重定向用户: if($this->request->method() == Request::POST) { $username = $this-&g

我的基本控制器具有以下功能:

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。这是因为会话正在通过基本控制器重置吗?我该怎么做才能避免这种情况?

为什么你要把简单的事情复杂化这么多?不需要使用会话进行错误处理

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();

@martino谢谢,不过我更愿意理解为什么会重置会话变量,以及是否有解决方法。尝试只删除2个重定向,然后看看发生了什么。谢谢。如果重定向到其他控制器,我需要更改什么才能保留会话值?会话/重定向允许用户在发布表单失败后刷新表单,而无需获得表单重新提交对话框。