【问题标题】:Is it possible to reset the displayed URL after a function call in Code Igniter?在 Codeigniter 中调用函数后是否可以重置显示 URL?
【发布时间】:2016-02-26 02:46:55
【问题描述】:

我在控制器 (public function Sort($field, $dir)) 中有一个函数,它根据指定方向上提供的字段对传入数据进行排序。用户通过单击绿色(或红色)箭头与显示的视图交互来访问此功能...

不幸的是,调用此函数后,URL 保留了进行函数调用的格式,这……有点烦人。

http://foo.barhttp://foo.bar/sort/FIELD/asc

在对表格进行排序的函数调用之后,我想返回到foo.bar,而不是在网络浏览器中显示foo.bar/sort/FIELD/asc...

我将如何实现这一目标?

【问题讨论】:

  • 长网址的一个优势——用户可以创建一个可以使用的书签。

标签: php codeigniter model-view-controller


【解决方案1】:

有几种不同的方法来做到这一点! (编程的美化,amirite?)


选项 1:使用 Flashdata 重定向

您可以重定向以使用原始 url (http://foo.bar/sort/FIELD/asc) 处理数据,然后执行重定向 set_flashdata,然后让原始 http://foo.bar 在加载前查找 flashdata。

function sort($field, $sort) {
    //do stuff
    $this->session->set_flashdata('sort_result', $sort_result);
}

function index() {
    if ($this->session->flashdata('sort_result')) {
        //do fanciness
    } else {
        //do normal things.
    }
}

缺点: 不得不直接/重定向的额外步骤可能会惹恼用户并且坦率地说看起来有点糟糕,flashdata 也可能需要传递 JSON 结果而不是完整的 PHP 对象。


选项 2:Ye Ole Javascript (AJAX):

使用 javascript 将数据加载到您的表中,这将使用户保持在同一页面上,但逻辑有点奇怪。

PHP 端:

function sort($field, $sort) {
    echo $this->load->view('sorted_table_view', $sort_result, TRUE);
}

function index() {
   //Normal
}

Javascript 端:

(function() {
   $.get('<?= site_url('sort/field/asc'); ?>', function(data) {
      //Data is the echoed view.
      $('#mydiv').html(data);
   });
});

无论如何,这只是您可以做到的两种快速方法。我敢肯定还有更多!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多