CodeIgniter - 显示索引函数的原始URL?

时间:2011-01-25 17:34:09

标签: codeigniter

我不确定我是否接近这种根本错误,或者我是否只是错过了什么。

我有一个控制器,其中有一个索引函数,显然是在调用该控制器时加载的默认值:

function index($showMessage = false) {
    $currentEmployee = $this->getCurrentEmployee();
    $data['currentEmp'] = $currentEmployee; 
    $data['callList'] = $currentEmployee->getDirectReports();
    $data['showMessage'] = $showMessage;
    $this->load->view('main', $data);
}

我在该控制器中有另一个执行批量更新的功能。更新完成后,我希望原始页面再次显示消息,所以我尝试了这个:

/**
* Will save all employee information and return to the call sheet page
*/
function bulkSave() {
    //update each employee
    for ($x = 0; $x < sizeof($_POST['id']); $x++) {
        $success = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
    }

    $this->index($success);                
}

发生的事情是使用以下方式访问原始页面: 本地主机/对myApp / myController的

批量更新后,它显示为: 本地主机/对myApp / myController的/ bulkSave

当我真的希望它再次将url显示为索引页面时,这意味着用户永远不会真正看到URL的/ bulkSave部分。这也意味着如果用户要刷新页面,它将调用控制器中的index()函数而不是bulkSave()函数。

提前致谢。

这可能吗?

2 个答案:

答案 0 :(得分:1)

我通常会重定向到上一页,因为它会阻止用户刷新(并提交两次)他们的数据。

您可以使用CI的redirect()辅助功能。

http://codeigniter.com/user_guide/helpers/url_helper.html(在底部)

答案 1 :(得分:1)

您正在index()内直接调用bulkUpdate()函数,因此uri不会更改回索引,因为您没有发出新的服务器请求,而只是在控制器类中导航。 / p>

我通常对这样的任务使用相同的Controller功能,根据是否已经传递$_POST数据来引导流量......

function index() {

    if($_POST) {

        //process posted data
        for ($x = 0; $x < sizeof($_POST['id']); $x++) {
            $data['showMessage'] = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
        }            
    }
    else {

        //show page normally
        $data['showMessage'] = FALSE;

    }

    //continue to load page
    $currentEmployee = $this->getCurrentEmployee();
    $data['currentEmp'] = $currentEmployee; 
    $data['callList'] = $currentEmployee->getDirectReports();
    $this->load->view('main', $data);

}

然后,如果它是您提交的表单,只需将表单指向您自己的视图中,就像这样......

<?= form_open($this->uri->uri_string()) ?>

这将表格重新指向索引,因为您通过$_POST发布表单数据,它将处理数据。