2011-12-09 49 views
4

我想知道如果有人能让我知道他们如何处理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'); 
} 

然后在同一个控制器(创建姿态),我有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,它会显示该页面。

如果有人可能会告诉我一个更好的方式来处理这个,它将不胜感激。

干杯,

+0

考虑使用闪光灯会话数据。只要检测到会话数据,打印警报或成功消息就会更加高效,而不是为每次成功/失败都创建单独的页面。 – user482594

回答

4

有没有你不使用这样的理由:

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

映入眼帘, 斯特凡

+0

嗯,这似乎是有道理的...这将消除额外的网段,而不会 – BigJobbies

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