简体   繁体   English

CodeIgniter-显示索引功能的原始URL?

[英]CodeIgniter - showing original URL of index function?

I'm not sure if I'm approaching this fundamentally wrong or if I'm just missing something. 我不确定是否要从根本上解决这个问题,还是只是缺少一些东西。

I have a controller and within it an index function that is, obviously, the default loaded when that controller is called: 我有一个控制器,在其中有一个索引函数,显然是调用该控制器时加载的默认函数:

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

I have another function within that controller that does a bulk update. 我在该控制器中还有另一个功能可以进行批量更新。 After the updates are complete, I want the original page to show again with the message showing, so I tried this: 更新完成后,我希望再次显示原始页面并显示消息,因此我尝试了以下操作:

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

What is happening is that the original page is accessed using: localhost/myApp/myController 发生的事情是使用以下命令访问了原始页面:localhost / myApp / myController

after the bulk update it is showing as: localhost/myApp/myController/bulkSave 批量更新后,它显示为:localhost / myApp / myController / bulkSave

when I really want it to show the url as the index page again, meaning that the user never really sees the /bulkSave portion of the URL. 当我真的希望它再次将URL显示为索引页时,意味着用户永远不会真正看到URL的/ bulkSave部分。 This would also mean that if the user were to refresh the page it would call the index() function in the controller and not the bulkSave() function. 这也意味着如果用户要刷新页面,它将在控制器中调用index()函数,而不是bulkSave()函数。

Thanks in advance. 提前致谢。

Is this possible? 这可能吗?

I usually do a redirect to the previous page as it prevent users to refresh (and submit twice) their data. 我通常会重定向到上一页,因为它阻止用户刷新(并提交两次)数据。

You can use the redirect() helper function of CI. 您可以使用CI的redirect()辅助函数。

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

You are calling your index() funciton directly, within bulkUpdate() hence the uri does not change back to index because you are not making a new server request, you are just navigating within your controller class. 您直接在bulkUpdate()中调用index() bulkUpdate()因此uri不会更改回index,因为您没有在发出新的服务器请求,只是在控制器类中导航。

I usually use the same Controller function for tasks like this, directing traffic based on whether or not $_POST data has been passed or not like this... 我通常对相同的任务使用相同的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);

}

Then if it is a form that you are submitting, just point the form at itself in your view like this... 然后,如果您要提交的是表单,则只需在您的视图中将表单本身指向这样即可...

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

This points the form back at index, and because you are posting form data via $_POST it will process the data. 这将表单指向索引,因为您通过$_POST发布表单数据,它将处理该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM