0

我目前的 URI 有问题

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

更新:

添加:

 $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');
                }
        }
4

1 回答 1

4

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

可以这样想:您所在的页面是users/add. 此页面显示的内容取决于您加载的视图。所以,即使你加载了user视图,你仍然在 page users/add

如果你想重定向,而不是

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

You need to do

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

However, in the latter case, you cannot pass data as you do when you load a view. If you want to retain data after a redirect, you will need to store it in session.

So, in your above code, where you create $data['message'], add it to the session:

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

When you redirect, at the other controller, retrieve the message, and remove it from session:

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

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

EDIT

In your edit, in the users controller, you are extracting the message from session. But, it's not available to the view. You still need to pass it on.

$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);
于 2012-06-14T05:35:14.633 回答