dongpingwu8378 2016-07-27 22:36
浏览 43

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

i've recently started using codeigniter and i'm having a few issues with my routing. I feel like i may have missed something in the way that it's meant to work, despite reading the documentation.

I've currently managed to route the default controller to execute the correct method in a class i've got, but when i try the same process for say, a second function with an argument, i get a Page not found error. Is my routing the issue here, or is there a problem with one of the methods?

Routing:

$route['(:any)'] = 'front/index/$1';
$route['/category/(:any)'] = 'front/category/$1';
$route['default_controller'] = 'front';

Database Model (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();
        }
}

Controller (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条回答 默认 最新

  • douluoxiao2286 2016-07-28 13:01
    关注

    Try these routes

    $route['category'] = 'front';
    $route['category/(:any)'] = 'front/category/$1';
    $route['default_controller'] = 'front';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计