【问题标题】:Is it possible to have the same CodeIgniter URI segment as a function parameter and a pagination parameter?是否可以将相同的 CodeIgniter URI 段作为函数参数和分页参数?
【发布时间】:2013-11-02 18:33:05
【问题描述】:

我有一个方法 news(),它接受两个可选参数 - $category$slug

新闻页面需要显示所有未分类新闻文章的分页列表(类别页面(带有$category 集)也需要这样做,但针对分类子集)。

因此,标准分页似乎不起作用,因为第二个 URI 段被视为news()$category 参数。是否有可能解决这个问题,如果它不是整数,则可能将第二个 URI 段视为 $category 参数,或者如果它是分页参数?

以下是相关的代码片段:

控制器

function news($category = null, $slug = null) {
    if($category == null) { // Get the standard "news" page

        // Define the pagination config
        $config = array();
        $config['base_url'] = base_url() . 'news/';
        $config['total_rows'] =$this->post_model->count_posts('NWS');
        $config['per_page'] = 3;
        $config['uri_segment'] = 2;
        $config['use_page_numbers'] = TRUE;

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        // Set the page info
        $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
        $data['newsPosts'] = $this->post_model->get_post_list_excerpt('NWS',$config['per_page'], $page);
        $data['links'] = $this->pagination->create_links();

        $this->template->load('default', 'newsview', $data);
    }
    elseif($slug == null) { 
        // Get the page specific to the chosen category
    }
}

为了尝试整理 URL,我也在使用路由:

routes.php

$route['news'] = 'site/news';
$route['news/(:any)'] = 'site/news/$1';
$route['news/(:any)/(:any)'] = 'site/news/$1/$2';

有没有办法绕过我正在尝试做的事情/甚至可能吗?我想避免使用单独的方法/控制器(如果可能的话,例如 news/categories/$category

【问题讨论】:

标签: codeigniter pagination codeigniter-2


【解决方案1】:

好的,这是您可以考虑的一些建议。

  1. 您可以使用base_url("site/news/"); 而不是base_url() . 'news/'; 来澄清您的代码。
  2. 在这种情况下使用news/(:any)/(:any) 正则表达式不明确/不正确,因为第一个(:any) 模式已经包含了url 的所有其余部分。我的意思:

    example.com/site/news/12/file

    • $route['news/(:any)'] = 'site/news/$1';

      $1 将匹配 12/file

    • $route['news/(:any)/(:any)'] = 'site/news/$1/$2';

      $1 将匹配:12/file
      $2 将匹配:(无)

    您是否可以考虑使用一些特定的通配符并为您的网址提供额外的安全性:

    注意:记住从最长到最短应用规则:

    $route['news/(:num)/([a-z]+)'] = 'site/news/$1/$2';
    $route['news/(:num)'] = 'site/news/$1';
    $route['news'] = 'site/news';
    

现在,回到最初的问题,我认为您可以反转参数以让category 作为最后一个。让我们看看:

$config['base_url'] = base_url("news/category/subset");
$config['uri_segment'] = 4;

看看这个:

public function news($category = false, $subset = false, $page = 1)
{
    $this->load->library('pagination');

    //checks if category is the page number
    if ((string)(int)$category === $category)
    {
        //ok, there is not category neither subset
        $config['base_url'] = base_url('site/news');
        $config['uri_segment'] = 3;
    }

    //checks if subset is the page number
    else if ((string)(int)$subset === $subset)
    {
        $config['base_url'] = base_url('site/news/' . $category);
        $config['uri_segment'] = 4;
    }

    //by elimination, all the three parameters are presents
    else
    {
        //ok, both are presents
        $config['base_url'] = base_url('site/news/' . $category . '/' . $subset);
        $config['uri_segment'] = 5;
    }

    $config['total_rows'] = 200;
    $config['per_page'] = 20;

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

    // more stuff here
}

此分页配置应与以下网址一起使用:

  • example.com/site/news/
  • example.com/site/news/cat/
  • example.com/site/news/cat/subset/

和页码:

  • example.com/site/news/3
  • example.com/site/news/cat/5
  • example.com/site/news/cat/subset/3

【讨论】:

  • 谢谢,马尼克斯。我提出了 #2 中的建议,这使事情有所整理。我也尝试了最后的建议,但没有奏效。问题是category/subset 并不总是存在。 URI 段的第二部分可以要么是类别,也可以是页码
  • 你能告诉我哪些参数是可选的吗?您是否对没有值的参数使用默认值?例如,当类别不存在时,可以将其设置为general
  • $category$slug 是可选参数,未设置时为空(即example.com/news/
  • 非常感谢!我做了一些调整来整理 URL(在各种 if ((string)(int)$subset === $subset) 子句之后添加 `|| $slug == null`,这样我就不必在每个 url 的末尾添加 /1),它就像一个魅力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 2018-11-30
  • 2013-01-14
  • 1970-01-01
  • 2018-06-23
  • 2013-01-16
相关资源
最近更新 更多