【发布时间】:2016-07-27 22:36:12
【问题描述】:
我最近开始使用 codeigniter,但我的路由遇到了一些问题。尽管阅读了文档,但我觉得我可能错过了它应该工作的方式。
我目前已设法路由默认控制器以在我拥有的类中执行正确的方法,但是当我尝试相同的过程时,例如,带有参数的第二个函数,我得到一个 Page not found 错误.是我的路由问题,还是其中一种方法有问题?
路由:
$route['(:any)'] = 'front/index/$1';
$route['/category/(:any)'] = 'front/category/$1';
$route['default_controller'] = 'front';
数据库模型(News_model.php)
class News_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_news_latest() {
$query = $this->db->get('chanl_posts', 20);
return $query->result_array();
}
public function get_news_category($category) {
$query = $this->db->get_where('chanl_posts', array('category' => $category));
return $query->row_array();
}
}
控制器(Front.php)
class Front extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function category($category)
{
$data['news_item'] = $this->news_model->get_news_category($category);
if (empty($data['news_item']))
{
show_404();
}
$this->load->view('templates/header', $data);
$this->load->view('front/index', $data);
$this->load->view('templates/footer');
}
public function index() {
$data['news'] = $this->news_model->get_news_latest();
$this->load->view('templates/header', $data);
$this->load->view('front/index', $data);
$this->load->view('templates/footer');
}
}
【问题讨论】:
-
你为什么要为你的 Index 传递一个参数? $route['(:any)'] = 'front/index/$1';顺便说一句,您的索引函数不期望任何参数 index()
标签: php codeigniter