Codeigniter URI页面重定向

时间:2012-06-14 05:26:04

标签: php codeigniter redirect controller uri

我目前的URI存在问题

示例我有一个名为add的函数 - users / add如果我的输入表单出错,它会在用户/添加页面上显示错误但如果一切都好,则会返回到用户页面,显示成功,但uri仍然/ users / add,我必须重新导航到用户页面。

更新

添加:

 $data['message'] = '<div class="alert alert-success"><strong>Thank You</strong> Your User Has Been Added</div>';
   $this->session->set_userdata('message', $data['message']);
   redirect(site_url('dashboard/users'));

用户:

$message = $this->session->userdata('message');

        if($message === FALSE)
        {
            $message='';
        }else{
            $this->session->unset_userdata('message');
        }

原始

控制器:

public function add()
        {
            $this->form_validation->set_rules('userFirstName','First Name', 'required|trim|max_length[99]|xss_clean');
            $this->form_validation->set_rules('userLastName','Last Name', 'required|trim|max_length[99]|xss_clean');
    $this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
            $this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[99]|xss_clean');

            if($this->form_validation->run() === TRUE)
                {
                    $userData = array(
                        'fName' => $this->input->post('userFirstName', TRUE),
                        'lName' => $this->input->post('userLastName', TRUE),
                        'email' => $this->input->post('userEmail', TRUE),
                        'password' => sha1($this->input->post('userPassword', TRUE))
                    );  

                    $this->db->escape($userData);
                    $this->user_model->addUser($userData);

                    $data['contentMangement'] = $this->options_model->systemOptions();
                    $data['pageTitle'] = 'Add User';
                    $data['message'] = '<div class="alert alert-success"><strong>Thank You</strong> Your User Has Been Added</div>';
                    $this->load->view('_assets/dashHeader', $data);
                    $this->load->view('dashboard/users', $data);
                    $this->load->view('_assets/footer');

                }elseif($this->form_validation->run() === FALSE)
                {
                    $data['contentMangement'] = $this->options_model->systemOptions();
                    $data['pageTitle'] = 'Add User';
                    $data['message'] = validation_errors('<div class="alert alert-error">', '</div>'); 
                    $this->load->view('_assets/dashHeader', $data);
                    $this->load->view('dashboard/addUser', $data);
                    $this->load->view('_assets/footer');
                }
        }

1 个答案:

答案 0 :(得分:4)

这是因为您没有从当前页面重定向。您只是从users/add加载“用户”视图。

以这种方式思考:您所在的页面是users/add。此页面显示的内容取决于您加载的视图。因此,即使您加载user视图,您仍然位于页面users/add

如果您想重定向,而不是

$this->load->view('dashboard/users',$data);

你需要做

redirect(site_url('users')); //or whatever your controller is called

但是,在后一种情况下,您无法像加载视图时那样传递数据。如果要在重定向后保留数据,则需要将其存储在会话中。

因此,在上面的代码中,您创建$ data ['message'],将其添加到会话中:

$this->session->set_userdata('message',$data['message']);

当您重定向时,在另一个控制器上检索该消息,并将其从会话中删除:

$message = $this->session->userdata('message');

if ($message === FALSE) {
   // message was not present in session
   $message = '';
} else {
    $this->session->unset_userdata('message');
}

修改

在您的编辑中,在users控制器中,您正在从会话中提取消息。但是,视图无法使用。你仍然需要传递它。

$message = $this->session->userdata('message');

if($message === FALSE)
{
    $message='';
} else {
    $this->session->unset_userdata('message');
}

//at this point, message is only available to the controller
// lets send it to the view

$data['message'] = $message;
$this->load->view('yourView',$data);