简体   繁体   English

分页的第一个链接显示 CodeIgniter 中的 404 页

[英]First link of pagination shows 404 page in CodeIgniter

I am working on pagination of category page.我正在处理类别页面的分页。 my route is我的路线是

$route['category/(:any)/(:num)'] = 'public_controller/category/$1';

And url to go to category page is http://localhost/ecom/category/seats but it shows 404 error page.而 url 到 go 到类别页面是http://localhost/ecom/category/seats但它显示 404 错误页面。 but when I give http://localhost/ecom/category/seats/1 it works fine and pagination also works fine second and third links are also working but when I click on 1 in pagination it shoes 404 error page.但是当我给http://localhost/ecom/category/seats/1时,它工作正常,分页也工作正常,第二个和第三个链接也工作,但是当我在分页中单击 1 时,它会出现 404 错误页面。 my controller code is我的 controller 代码是

   public function category($cat_slug = null,$offset = null){
 $cat_id = $this->db->where('cat_slug',$cat_slug)->get('categories')->row_array();
 $config['base_url'] = base_url() .'category/'.$cat_slug;
    $config['total_rows'] = $this->db->where('cat_id',$cat_id['cat_id'])->count_all('products');
    $config['per_page'] = 4;
    $config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] ="</ul>";
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['next_tag_open'] = "<li>";
    $config['next_tagl_close'] = "</li>";
    $config['prev_tag_open'] = "<li>";
    $config['prev_tagl_close'] = "</li>";
    $config['first_tag_open'] = "<li>";
    $config['first_tagl_close'] = "</li>";
    $config['last_tag_open'] = "<li>";
    $config['last_tagl_close'] = "</li>";

    $this->pagination->initialize($config);
 $data['products'] = $this->public_model->category_page($cat_slug,$config['per_page'],$offset);
 $id['cat_slug'] = $cat_slug;
 $this->load->view('public_temp/header1');
 $this->load->view('public_temp/left1',$id);
 $this->load->view('public/category',$data);
 $this->load->view('public_temp/footer1');
}

What can I try to resolve this?我可以尝试什么来解决这个问题?

It shows because your router expect 2 parameter but you are giving 1 parameter seats它显示是因为您的router expect 2 parameter but you are giving 1 parameter seats

Define your route this way以这种方式定义您的路线

$route['category/(:any)/(:num)'] = 'public_controller/category/$1/$2';

Now call your route http://localhost/ecom/category/seats/0 for category page http://localhost/ecom/category/seats/1 for first pagination现在调用您的路线http://localhost/ecom/category/seats/0用于类别页面http://localhost/ecom/category/seats/1用于第一个分页

if you want to separate your category route Define two route one for category page and other for category pagination如果你想分开你的类别路线Define two route one for category page and other for category pagination

For category page对于类别页面

$route['category/(:any)'] = 'public_controller/category/$1';

For category pagination对于类别分页

$route['category/(:any)/(:num)'] = 'public_controller/category/$1/$2';

So, I created a demo for your problem and this is what worked for me ↓↓所以,我为你的问题创建了一个演示,这对我有用↓↓

$route['category']               = 'my_controller/category';       // this route handles all the requests without any additional segments
$route['category/(:any)']        = 'my_controller/category/$1';    // this route handles the requests when only one segment is present
$route['category/(:any)/(:num)'] = 'my_controller/category/$1/$2'; // this route handles the requests when there are two segments(2nd parameter being a number)

This is possible because这是可能的,因为

Routes will run in the order they are defined.路由将按照它们定义的顺序运行。 Higher routes will always take precedence over lower ones.( Reference )较高的路线总是优先于较低的路线。(参考

I also found a similar question you might wanna take a look at.我还发现了一个类似的问题,您可能想看看。

Hope this resolves your issue.希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM