简体   繁体   English

CodeIgniter新闻站点(针对不同功能的路由索引)

[英]CodeIgniter News Site (Routing Index to different functions)

i've recently started using codeigniter and i'm having a few issues with my routing. 我最近开始使用codeigniter,但我的路由遇到了一些问题。 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) 数据库模型(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) 控制器(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');
        }
}

Try these routes 试试这些路线

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM