1

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

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

$category因此,标准分页似乎不起作用,因为第二个URI段被视为news(). 是否有可能解决这个问题,如果它不是整数,则可能将第二个 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,我还使用了路由:

路由.php

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

有没有办法解决我正在尝试做的事情/甚至可能吗?我想避免必须有单独的方法/控制器(news/categories/$category如果可能的话

4

1 回答 1

0

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

  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/文件

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

      $1将匹配:12/文件
      $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
于 2013-11-03T04:58:59.230 回答