【问题标题】:CodeIgniter - showing original URL of index function?CodeIgniter - 显示索引函数的原始 URL?
【发布时间】:2011-01-25 17:34:09
【问题描述】:

我不确定我是否从根本上错误地解决了这个问题,或者我只是错过了一些东西。

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

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

批量更新后显示为: localhost/myApp/myController/bulkSave

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

提前致谢。

这可能吗?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

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

    我通常对这样的任务使用相同的 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 发布表单数据,它将处理数据。

    【讨论】:

      【解决方案2】:

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

      您可以使用 CI 的 redirect() 辅助函数。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-15
        • 2016-12-04
        相关资源
        最近更新 更多