CodeIgniter Project中的路由问题

时间:2015-12-15 18:21:09

标签: php codeigniter pagination

我一直致力于代码Igniter项目,我差不多完成了。我最后添加了分页但有一个问题。当我滚动浏览页面上的分页链接,然后单击转到我的主页时,收到错误消息。试图打破这一点,以便其他人可以更好地理解它,我的博客页面,分页有这个URL:

proj1 / index.php的/博客

主页的网址是:

proj1 / index.php的/家

当我点击我的分页链接“2”,显示下一组博客时,我得到了这个网址:

proj1 / index.php的/博客/ 2

现在,如果我在博客/ 2上,然后点击“主页”,我会在这里:

proj1 / index.php的/博客/家

如果我去我的简历页面,我会看到:

proj1 / index.php的/博客/工作

基本上,我不会回到我的主页,也不会回到我的简历页面。有人能帮忙解决这个问题吗?谢谢您的帮助!!

这是我的博客控制器的样子:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Blog extends CI_Controller {

    //This function begins to construct the controller. 
    public function __construct()

      {
        parent::__construct();
        $this->load->model('Blog_model');
        $this->load->helper('url_helper');
        //pagination being loaded from the library 
        $this->load->library('pagination');
      }

    public function index()

    {
      //This line sets the page for the base URL
      $config['base_url'] = base_url('index.php/blog');
      $config['total_rows'] = $this->Blog_model->count_items();
      $config['per_page'] = 2;
      $this->pagination->initialize($config); 

      $data['title'] = 'Blog archive';


      $data['pagination'] = $this->pagination->create_links();
      $start = '';

      $slug = '';

      $data['blog'] = $this->Blog_model->get_items($config['per_page'],                           jjjj      $this->uri->segment(2));

      $this->load->view('templates/header', $data);
      $this->load->view('blog/index', $data);
      $this->load->view('templates/footer');
    }

    public function view($slug = NULL)

    {

      $data['blog_item'] = $this->Blog_model->get_blog($slug);

      if (empty($data['blog_item']))
        {
          show_404();
        }

      $data['title'] = $data['blog_item']['title'];

      $this->load->view('templates/header', $data);
      $this->load->view('blog/view', $data);
      $this->load->view('templates/footer');

    }

    public function create()

    {
      $this->load->helper('form');
      $this->load->library('form_validation');

      $data['title'] = 'Create a Blog Entry';

      $this->form_validation->set_rules('title', 'Title', 'required');
      $this->form_validation->set_rules('body', 'Body', 'required');

      if ($this->form_validation->run() === FALSE)

        {
          $this->load->view('templates/header', $data);
          $this->load->view('blog/create');
          $this->load->view('templates/footer');

        }
        else

        {
          $this->Blog_model->set_blog();
          redirect('blog');
        }
    }
     }

这是我的博客模型:

    <?php
    class Blog_model extends CI_Model {

    //This function connects to the database and loads it. 
    public function __construct()

    {
    parent::__construct();
    $this->load->database();
    }

     //This function counts all of the items in the blog table. 
    public function count_items() 

     { 
     return $this->db->count_all('blog');
     }

       //function pulls items out of the array specifically by newest date                      first. 
    function get_items($limit, $offset) 

    {
    $data = array();
    $this->db->limit($limit, $offset);
    $this->db->order_by('entry_date', 'desc');
    $query = $this->db->get('blog');
    if ($query->num_rows() > 0){
    foreach ($query->result_array() as $row){
    $data[] = $row;
     }
    }
   $query->free_result();
   return $data;
   }

   public function set_blog()

    {
  $this->load->helper('url');

  $slug = url_title($this->input->post('title'), 'dash', TRUE);

  $data = array(
  'title' => $this->input->post('title'),
  'slug' => $slug,
  'body' => $this->input->post('body')
  );

    return $this->db->insert('blog', $data);
    }
    }

最后,我的路线看起来像这样:

$route['message'] = 'contact/create';
$route['contact'] = 'contact';
$route['blog/(:any)'] = 'blog/index/$1';
$route['create'] = 'blog/create';
$route['blog'] = 'blog';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

感谢您的帮助,如果您需要我发布更多信息,请告诉我们。我不认为这是一个难以解决的问题。但是,它让我难过!再次,谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

如评论中所述,您应该更改

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

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

我会将控制器中的索引方法更改为page,然后将config base_url更改为:

$config['base_url'] = base_url('index.php/blog/page/');

你可以改变路线:

$route['blog'] = 'blog/page/1';

您可以删除以下路线,因为不再需要:

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

它应该都可以。

编辑: 我相信404是因为分页类检查了错误的url段。以下Mayby也可以提供帮助(尝试不同的值)。

$config['uri_segment'] = 2;