提问者:小点点

使用路由和常量文件的Codeigniter自定义分页url


我想自定义我的codeigniter分页url,“http://www.domanin.com/products.html”此url工作正常,但当我转到第2页(http://www.domanin.com/products.html/2)时,我发现没有找到页面。

默认情况下,codeigniter提供此url

www.domanin.com/controller/method/pageno

我希望它是这样的

http://www.domanin.com/products.html,第1页和

第二页我想要这样的

http://www.domanin.com/products.html/2

我正在使用constant.php和route.php文件进行路由。 constant.php文件代码

define('PRODUCTS', 'products.html');

route.php文件代码

$route[PRODUCTS] = 'ProductsController/index';

ProductsController.php文件代码

   $this->load->library('pagination');
   $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
   $config = [
        'base_url' => base_url('products.html'),
        'per_page' => 3,
        'total_rows' => $this->model->getAllProductsCount(),
    ];

    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['num_tag_open'] = '<li class="page-item">';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['next_tag_open'] = '<li class="page-item">';
    $config['next_tagl_close'] = '</a></li>';
    $config['prev_tag_open'] = '<li class="page-item">';
    $config['prev_tagl_close'] = '</li>';
    $config['first_tag_open'] = '<li class="page-item disabled">';
    $config['first_tagl_close'] = '</li>';
    $config['last_tag_open'] = '<li class="page-item">';
    $config['last_tagl_close'] = '</a></li>';
    $config['attributes'] = array('class' => 'page-link');
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config); // model function
    $template['campaigns'] = $this->model->getAllProducts($config['per_page'],$page);

我曾试图找到解决方案,所以有很多解决方案的分页,但没有一个帮助我。


共1个答案

匿名用户

您可能需要更新路由,因为您正在手动定义此URLroute

routes.php

$route[PRODUCTS]           = 'ProductsController/index';
$route[PRODUCTS.'/(:num)'] = 'ProductsController/index';

上面的代码告诉服务器,如果在PRODUCTS.html(PRODUCTS)后面有任何数字,您不需要查找它,而是将它重新路由到ProductsController控制器索引函数

希望能帮到你。