2015-12-15 139 views
0

我一直在研究一個代碼Igniter項目,我差不多完成了。我最後添加了分頁,但有一個問題。當我滾動瀏覽我的頁面上的分頁鏈接,然後點擊進入我的主頁時,我收到一條錯誤消息。試圖打破這種下來,讓其他人可以更好地瞭解它,我的博客頁面,其中有分頁有這個網址:在CodeIgniter項目中的路由問題

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

謝謝任何幫助,請讓我知道,如果你需要我發佈更多信息。我不認爲這是一個難以解決的問題。但是,它已經讓我難住了!再次感謝您的幫助!

+0

你已經定義了$ config ['base_url'] = base_url('index.php/blog');其中設置base_url –

+0

我可以知道你用什麼代碼片段鏈接到主頁 –

+0

我確實認爲這個問題可能在$ config變量中,但我該如何解決它? 「代碼段鏈接到主頁」是什麼意思?我確實使用這個:index.php/home是你的意思嗎? –

回答

0

正如在評論中提到,你應該改變

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

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

我會改變在控制器索引方法頁,比更改配置BASE_URL到:

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

你可以改變路線:

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

比你可以刪除以下路線,因爲它不再是必要的:

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

比它應該所有的工作。

編輯: 我相信404是因爲分頁類檢查了錯誤的網段。梅比以下也可以幫助(嘗試不同的值)。

$config['uri_segment'] = 2; 
+0

美好的一天,謝謝你的信息。我做了以下更改:$ config ['base_url'] = base_url('index.php/blog/page /');和我現在有的路線:$ route ['blog'] ='blog/page/1';但是,它仍然無法正常工作,每當我嘗試前往我的博客頁面時,我都會收到404錯誤消息。看起來,路線:$ route ['blog'] ='blog';需要!感謝您的幫助! –