繼隨着CI News TutorialCodeIgniter的路由,404錯誤
我只是做了新聞欄目,所以我改變了默認控制器「新聞」
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['default_controller'] = 'news';
現在從生成一個404錯誤'查看文章'錨點。將默認值更改爲:
$route['default_controller'] = 'welcome';
創建正確的路徑。我應該如何改變路由器使用新聞?
沒有自定義配置或使用.htaccess。
從config.php文件:
$config['base_url'] = 'http://frameworks:8888/ci_site_tut/';
$config['index_page'] = 'index.php';
新聞控制器:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News Archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create A News Item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
SOLUTION:
自動加載URL助手:
$autoload['helper'] = array('url');
級更新的路由:
$route['default_controller'] = 'news';
$route['404_override'] = 'errors/page_missing';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
的意見/新聞/ index.php文件更新網址:
<p><a href="<?php echo site_url('news/' . $news_item['slug']); ?>">View Article</a></p>
你想在瀏覽器中使用什麼網址? – Catfish 2012-01-03 17:04:47
View article
。 index.php不包含在URL中。 – Wasabi 2012-01-04 17:39:54我有同樣的問題....我認爲這是一個錯字。我把' 2013-10-15 23:50:34