Codeigniter 3 分页 links 和单个 post link 抛出 404

Codeigniter 3 pagination links and single post link throw 404

我已经从这里阅读了其他关于此的文章,但无法解决这个问题。我的问题是 2.

  1. 分页显示,但点击 link 分页生成 404。
  2. 我在数据库中有正在显示的文章列表,但是当我 点击单篇文章link进入单篇文章页面,显示404.

我在本地主机 wamp 上。这是我的代码。注释代码是我一直在尝试但对我不起作用的代码。

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond  !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L,QSA]
</IfModule>

routes.php

$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;

$route['articles/(:any)'] = 'articles/';
//$route['articles'] = 'articles';

config / baseurl

$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

控制器 - Articles.php

class Articles extends CI_Controller {

public function index($start=0)
{
    // $this->output->cache('86400');
    $this->load->view('header');
    //load model
    // $this->load->model('articles_model');
    // load 'get_articles' function from 'articles_model' model and store it in data

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

    $data['articles']=$this->articles_model->get_articles(5,$start);

    $config['base_url'] = base_url().'articles/';
    $config['total_rows'] = $this->db->get('articles')->num_rows();
    $config['per_page'] = 5;
    $config["uri_segment"] = 3;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = 2; //NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY

    //config for bootstrap pagination class integration
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = "&lt;&lt; First";
    $config['last_link'] = "Last &gt;&gt;";
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

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

    //$data['pages'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    //$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

    // //call the model function to get the department data
    //$data['pages'] = $this->articles_model->get_articles($config["per_page"], $data['page']);
    $data['pages'] = $this->pagination->create_links();


    //$this->load->view('page_articles');
    // load view and load model data with it 
    $this->load->view('page_articles',$data);

    $this->load->view('footer');
}


// single article
// function post($perma){
//  $this->load->view('header');
//  $this->load->model('articles_model');
//  $data['articles']=$this->articles_model->get_single_article($perma);
//  $this->load->view('page_article',$data);
//  $this->load->view('footer');
// }
}

型号/Articles_model

class Articles_model extends CI_Model {

// function get_articles() {
public function get_articles($limit, $start) {

    // $query = $this->db->query("SELECT * FROM articles");
    // $this->db->select()->from('articles');
    // $this->db->select()->from('articles')->where('active',1)->order_by('date_added','desc')->limit(0,20);

    $this->db->select()->from('articles')->where('status','1')->order_by('date_added','desc')->limit($limit, $start);
    $query = $this->db->get();
    // return object
    // return $query->result();
    // return array
    return $query->result_array();

}

function get_articles_count(){
    $this->db->select('id')->from('articles')->where('status',1);
    $query = $this->db->get();
    return $query->num_rows();
}



// for single article
// function get_single_article($perma){
//  $this->db->select()->from('articles')->where(array('status'=>1,'permalink'=>$perma));
//  $query = $this->db->get();
//  return $query->first_row('array');
// }


}

查看/page_articles

<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else 
{ 

  ?>

      <ol>
          <?php 
          foreach($articles as $row) 
          { ?>

                <li>
                    <h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
                        <?php echo $row['name']; ?>
                    </a></h4>
                    <p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
                    <br>
                    <a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">Read more</a>
                    </p>
                    <br>
                </li>

          <?php } ?>
      </ol>

      <?php
}
?>
</p>

<div>
    <?php echo $pages; ?>
</div>

一个问题...

public function index($start=0)

您似乎使用 $start 作为您的页码并再次使用您的 offset 值...

$data['articles']=$this->articles_model->get_articles(5,$start);

如果 $start 代表您的页码,那么您不能将它也用于您的 offset 值。

需要计算您的偏移量...

$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);

示例:

每页 5 条记录,第 4 页将从记录 #16 开始。因此 15 是正确的 offset 以获取第 4 页的记录 #16、17、18、19 和 20。


另一个潜在问题...

$config["uri_segment"] = 3;

您已将页码设置为第 3 段,但您的路线在第 2 段显示页码...

$route['articles/(:any)'] = 'articles/';

它也不符合此设置,这也会将您的页码放在第 2 段...

$config['base_url'] = base_url().'articles/';

基于

waow. What I did just now... I changed $config['base_url'] = base_url().'articles/'; to $config['base_url'] = base_url().'articles/index/'; and the pagination links show the page now but records are not changing. The 404 goes away and page is changing - records are first 5 on all pages....

那么路线应该是这样的...

$route['articles/index/(:any)'] = 'articles/';

因此它与您描述的新 base_url 匹配...

$config['base_url'] = base_url().'articles/index/';