dongyidao1461 2015-12-15 18:21
浏览 40

CodeIgniter Project中的路由问题

I have been working on a code Igniter project and I am almost done. I finally added Pagination but have one issue. When I scroll through my pagination links on my page and then click to go to my home page, I get an error message. Trying to break this down so that others can better understand it, my blog page, which has pagination has this URL:

proj1/index.php/blog

my URL for the home page is:

proj1/index.php/home

when I click on my pagination link for "2" which shows the next set of blogs, I got to this URL:

proj1/index.php/blog/2

Now if I am on blog/2 and then click "home" it takes me here:

proj1/index.php/blog/home

if I go to my resume page I see this:

proj1/index.php/blog/work

Basically, I am not going back to my home page nor my resume page. Is anyone able to help fix this problem? Thank you for your help!!

Here is what my blog controller looks like:

    <?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');
        }
    }
     }

Here is my blog model:

    <?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);
    }
    }

Finally, my routes look like this:

$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';

Thank you for any help and please let me know if you need me to post more information. I do not think that this is a hard issue to fix. However, it has stumped me! Again, thank you for the help!

  • 写回答

1条回答 默认 最新

  • doujiu9172 2015-12-16 00:29
    关注

    As mentioned in the comments you should change the

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

    to

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

    I would change the index method in the controller to page and than change the config base_url to:

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

    You can than change the route:

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

    Than you can remove the following route since it is no longer necessary:

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

    Than it should all work.

    Edit: I believe the 404 is because the pagination class checked the wrong url segment. Mayby the following could also help (try different values).

    $config['uri_segment'] = 2;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等