【问题标题】:Using URI Routing Wildcards使用 URI 路由通配符
【发布时间】:2013-02-19 23:03:53
【问题描述】:

在名为 article 的控制器中,我有以下函数,用于显示数据库中的数据:

function get_all(){

  $this->load->library('pagination');
  $config['base_url'] = base_url().'article/get-all';
  $config['total_rows'] = 2; // example
  $config['per_page'] = 1;
  $config['num_links'] = 20;
  $config['full_tag_open'] = '<div class="pagination" align="center">';
  $config['full_tag_close'] = '</div>';

  $this->pagination->initialize($config);

  $this->load->model('mod_articles');
  $data['records']= $this->mod_articles->get_articles_this_year($config['per_page'],$this->uri->segment(3));

  $this->load->view('view_article_list',$data);
}

通常到达函数的 url 是:http://localhost/article/get_all,但是为了使 url 看起来像这样 http://localhost/article/get-all,在 config/route.php 我添加了以下行:

 $route['article/get-all'] = "article/get_all";

它工作正常,但是当我点击分页链接转到下一页时,它显示 404 Page Not Found

能否请您告诉我如何正确使用 URI 路由通配符?

编辑

我在 config/route.php 中也有以下内容,以避免索引出现在 url 中

 $route['article/(:any)'] = "article/index/$1";

如果我删除上面的行,那么分页就可以了。

【问题讨论】:

    标签: codeigniter codeigniter-2


    【解决方案1】:

    分页类是否向链接添加任何额外的 URI 段?例如:

    /article/get-all/page/5

    如果是这样,您应该设置两条路线,例如:

    $route['article/get-all'] = "article/get_all";
    $route['article/get-all/(:any)/(:any)'] = "article/get_all/$1/$2";
    

    【讨论】:

    • 感谢您的回答。它不会像您提到的那样添加任何额外的 uri 段。我将鼠标悬停在分页链接上,我看到网址看起来像这样 article/get-all/1 。所以我使用了这个 $route['article/get-all/(:any)'] = "article/get_all/$1"; ,但它仍然显示 404 Page Not Found 。感谢您的帮助:)
    【解决方案2】:

    我没有看到您包含 uri_segment 的配置,分页库使用此配置。

    $config["uri_segment"] = 3;

    $page = ($this-&gt;uri-&gt;segment(3)) ? $this-&gt;uri-&gt;segment(3) : 0;

    把它当作

     $data['records']= $this->mod_articles->get_articles_this_year($config['per_page'],$page);
    

    在查询中直接附加 $this-&gt;uri-&gt;segment(3) 会返回 NULL,因为如果 url 段无效,它会返回 FALSE,而 sql limit 需要的是整数从哪里开始。

    【讨论】:

    • 非常感谢您的回答。我使用了您的代码,但仍然显示404 Page Not Found。 :(
    • @black_belt 悬停在链接上时,链接会将您重定向到哪里?
    • 如果我点击任何分页链接,它会将我带到article/error。我忘了提到我的 route.php $route['article/(:any)'] = "article/index/$1"; 中也有这个,如果我删除它,那么分页就可以了。但我真的需要摆脱 url 中的索引。谢谢:)
    • 如果你想摆脱index.php你需要.htaccess
    • article/index/$1 中的索引是控制器类中的一个函数 :) 我已经摆脱了 index.php :)
    猜你喜欢
    • 2019-06-24
    • 1970-01-01
    • 2011-06-18
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多