CodeIgniter -显示成功或不成功消息


CodeIgniter - displaying success or unsuccess message

我想知道是否有人可以让我知道他们如何处理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,它会显示页面。

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

欢呼,

你可以在重定向前设置一个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);
}

你为什么不使用这个呢?

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

问候,stefan

与其重定向,不如显示不同的视图

下面是一些示例代码:
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)
}