CodeIgniter新闻网站(路由索引到不同的功能)

时间:2016-07-27 22:36:12

标签: php codeigniter

我最近开始使用codeigniter,我的路由问题也有些问题。尽管阅读了文档,但我觉得我可能已经错过了它的工作方式。

我目前设法路由默认控制器以在我已经获得的类中执行正确的方法,但是当我尝试使用相同的过程时,第二个带有参数的函数,我得到一个页面未找到错误。我的路由问题在这里,或者其中一个方法有问题吗?

路由:

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

1 个答案:

答案 0 :(得分:0)

尝试这些路线

$route['category'] = 'front';
$route['category/(:any)'] = 'front/category/$1';
$route['default_controller'] = 'front';
相关问题