CodeIgniter - 显示成功或失败消息

时间:2011-12-09 07:50:26

标签: php codeigniter

我想知道是否有人可以告诉我他们如何处理CodeIgniter中的成功/失败消息。

例如,当用户注册我的网站时,我正在做什么,这就是控制器中发生的事情

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    // Redirect to the successful controller
    redirect( 'create-profile/successful' );
} else {
    // Redirect to the unsuccessful controller
    redirect( 'create-profile/unsuccessful' );
}

然后在同一个控制器(create-profile)中,我有2个方法,如下所示

function successful()
{
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

问题在于我可以直接访问site.com/create-profile/successful,它会显示该页面。

如果有人能够告诉我更好的处理方法,我们将不胜感激。

干杯,

3 个答案:

答案 0 :(得分:6)

您可以在重定向之前设置flashdata

$this->session->set_flashdata('create_profile_successful', $some_data);
redirect( 'create-profile/successful' );

function successful(){
    if( FALSE == ($data = $this->session->flashdata('create_profile_successful'))){
        redirect('/');  
    }

    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

答案 1 :(得分:5)

有没有理由不使用它:

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
} else {
    $data['h1title'] = 'Unsuccessful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

映入眼帘, 斯特凡

答案 2 :(得分:2)

而不是重定向,只显示不同的视图。

以下是一些示例代码:

if ($this->input->server('REQUEST_METHOD') == 'POST')
{
    // handle form submission
    if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') )))
    {
        // show success page
        $data['h1title'] = 'Successful Registration';
        $data['subtext'] = '<p>Test to go here</p>';

        // Load the message page
        $this->load->view('message',$data)
    }
    else
    {
        // show unsuccessful page
        $data['h1title'] = 'Unsuccessful Registration';
        $data['subtext'] = '<p>Test to go here</p>';

        // Load the message page
        $this->load->view('message',$data)
    }
}
else
{
    // show login page
    $data['h1title'] = 'Login';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('login',$data)
}
相关问题