【问题标题】:Codeigniter Pagination - 404 ErrorCodeigniter 分页 - 404 错误
【发布时间】:2013-06-22 17:50:58
【问题描述】:

我正在尝试对主页上的博客文章使用分页:

控制器:

public function index()
{
   $data['view'] = "home";

    /** Pagination **/
    $config['base_url'] = base_url().'home/';
    $config['total_rows'] = $this->Blog_model->count_articles();
    $config['per_page'] = 2; 
    $config['uri_segment'] = 2;

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

    $data['paginate'] = $this->pagination->create_links();
    $data['articles'] = $this->Blog_model->get_articles($config['per_page'],$this->uri->segment(2));

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

}

信息检索和分页似乎一切正常,但是,当我单击数字链接或下一个链接时,我收到 404 Not Found 错误。

我假设这与 URI 段有关?

正在加载的 URL 是第二页的 http://www.example.com/home/2

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-url


    【解决方案1】:

    您还可以添加如下路由规则:

    $route['home/(:num)'] = 'yourhomecontroller';
    

    然后你可以在没有索引或任何方法的情况下使用它,num 告诉它将 home/ 之后带有数字的任何 url 路由到 home 索引

    【讨论】:

      【解决方案2】:

      Codeigniter 页面的格式为 http://domain.com/controller/method/arguments
      如果您省略了默认方法,则会加载默认方法,但如果您需要传递参数,则必须将方法原样放置。

       http://www.example.com/home/index/2
      

      【讨论】:

      • 完美,谢谢,有没有办法摆脱索引?我只是在考虑搜索引擎优化?
      • 你可以尝试用mod_rewrite重写url
      【解决方案3】:

      控制器代码

      public function index()
      {
      
      $home=$this->uri->segment(2);
      
      $limit_ti=2;
      if(!$home):
      offset_ti = 0;
      else:
      $offset_ti = $home;
      endif;
      
      
      $this->load->model('Blog_model');// call model
      $this->load->library('pagination'); // call library
      
      $query=$this->Blog_model->get_articles($limit_ti,$offset_ti); // geting aan article 
      
      $total_page = $this->Blog_model->count_articles(); // count row
      
       /** Pagination **/
       $config['base_url'] = base_url().'index.php/home/'; // assuming you doesn't have htaccess
       $config['total_rows'] =$total_page->num_rows();
       $config['per_page'] = 2; 
       $config['uri_segment'] = 2;
      
          $this->pagination->initialize($config);
          $data = array('query' => $query,'page'=>$home);
      
          $data['view'] = "home";
          $this->load->view('template', $data);
      
      }
      

      型号代码

      function get_articles($limit,$ofset){
      $query=$this->db->query("select * from *table* order by id ASC LIMIT $ofset,$limit");
      return $query;
      }
      
      function count_articles(){
      $query=$this->db->query("select * from table");
      return $query;
      }
      

      在视图中

      <?php echo $this->pagination->create_links(); // call the pagnation?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        相关资源
        最近更新 更多