我一直在研究一個代碼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';
謝謝任何幫助,請讓我知道,如果你需要我發佈更多信息。我不認爲這是一個難以解決的問題。但是,它已經讓我難住了!再次感謝您的幫助!
你已經定義了$ config ['base_url'] = base_url('index.php/blog');其中設置base_url –
我可以知道你用什麼代碼片段鏈接到主頁 –
我確實認爲這個問題可能在$ config變量中,但我該如何解決它? 「代碼段鏈接到主頁」是什麼意思?我確實使用這個:index.php/home是你的意思嗎? –