Codeigniter 管理路由

Codeigniter admin route

我被这个问题困住了。 尝试访问 codeigniter 中的管理页面时出现 404 错误,我只能访问前端页面。 这是我的 route.php 文件:

$route['default_controller'] = "page";
$route['404_override'] = '';
$route[':any'] = "page/index/"; **WORKS**
$route['admin/page'] = "admin/page"; **WORKS**
$route['admin/page/order'] = "admin/page/order"; **NOT WORKING**  <!-- this i my issue -->
$route['admin/page/edit'] = "admin/page/edit"; **WORKS**
$route['admin/page/edit/(:num)'] = "admin/page/edit/"; **NOT WORKING**  <!-- this i my issue -->

管理员控制器

class Admin_Controller extends MY_Controller
{

    function __construct ()
    {
        parent::__construct();
        $this->data['meta_title'] = 'My awesome CMS';
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('user_m');

        // Login check
        $exception_uris = array(
            'admin/user/login', 
            'admin/user/logout'
        );
        if (in_array(uri_string(), $exception_uris) == FALSE) {
            if ($this->user_m->loggedin() == FALSE) {
                redirect('admin/user/login');
            }
        }

    }
}

前端页面格式

http://www.my_site.com/index.php/about

后端格式

http://www.my_site.com/admin/page
http://www.my_site.com/index.php/admin/page/edit/1

拜托,谁能帮帮我? 提前致谢。

各位谢谢回复 这里有我的 Page Admin Controller 并且我还修改了上面的 route.php。

<?php
class Page extends Admin_Controller
{

    public function __construct ()
    {
        parent::__construct();
        $this->load->model('page_m');
    }

    public function index ()
    {
        // Fetch all pages
        $this->data['pages'] = $this->page_m->get_with_parent();

        // Load view
        $this->data['subview'] = 'admin/page/index';
        $this->load->view('admin/_layout_main', $this->data);
    }

    public function order ()
    {
        $this->data['sortable'] = TRUE;
        $this->data['subview'] = 'admin/page/order';
        $this->load->view('admin/_layout_main', $this->data);
    }

    public function order_ajax ()
    {
        // Save order from ajax call
        if (isset($_POST['sortable'])) {
            $this->page_m->save_order($_POST['sortable']);
        }

        // Fetch all pages
        $this->data['pages'] = $this->page_m->get_nested();

        // Load view
        $this->load->view('admin/page/order_ajax', $this->data);
    }

    public function edit ($id = NULL)
    {
        // Fetch a page or set a new one
        if ($id) {
            $this->data['page'] = $this->page_m->get($id);
            count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
        }
        else {
            $this->data['page'] = $this->page_m->get_new();
        }

        // Pages for dropdown
        $this->data['pages_no_parents'] = $this->page_m->get_no_parents();

        // Set up the form
        $rules = $this->page_m->rules;
        $this->form_validation->set_rules($rules);

        // Process the form
        if ($this->form_validation->run() == TRUE) {
            $data = $this->page_m->array_from_post(array(
                'title', 
                'slug', 
                'body', 
                'template', 
                'parent_id'
            ));
            $this->page_m->save($data, $id);
            redirect('admin/page');
        }

        // Load the view
        $this->data['subview'] = 'admin/page/edit';
        $this->load->view('admin/_layout_main', $this->data);
    }

    public function delete ($id)
    {
        $this->page_m->delete($id);
        redirect('admin/page');
    }

    public function _unique_slug ($str)
    {
        // Do NOT validate if slug already exists
        // UNLESS it's the slug for the current page


        $id = $this->uri->segment(4);
        $this->db->where('slug', $this->input->post('slug'));
        ! $id || $this->db->where('id !=', $id);
        $page = $this->page_m->get();

        if (count($page)) {
            $this->form_validation->set_message('_unique_slug', '%s should be unique');
            return FALSE;
        }

        return TRUE;
    }
}

前端页面控制器

<?php

class Page extends Frontend_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('page_m');

    }

    public function index(){
        // Fetch the page template
        $this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
        count($this->data['page']) || show_404(current_url());

        // Fetch the page data
        $method = '_' . $this->data['page']->template;
        if (method_exists($this, $method)) {
            $this->$method();
        }
        else {
            log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
            show_error('Could not load template ' . $method);
        } 
        //Fetch the view
        $this->data['subview'] = $this->data['page']->template;
        $this->load->view('_main_layout', $this->data);
    }

    private function _page(){
         dump('welcome from the page template');
    }

     private function _homepage(){
        $this->load->model('article_m');
        $this->db->where('pubdate <=', date('Y-m-d'));
        $this->db->limit(6);
        $this->data['articles'] = $this->article_m->get();
    }

     private function _news_archive(){
         $this->load->model('article_m');

        // Count all articles
        $this->db->where('pubdate <=', date('Y-m-d'));
        $count = $this->db->count_all_results('articles');

        // Set up pagination
        $perpage = 4;
        if ($count > $perpage) {
            $this->load->library('pagination');
            $config['base_url'] = site_url($this->uri->segment(1) . '/');
            $config['total_rows'] = $count;
            $config['per_page'] = $perpage;
            $config['uri_segment'] = 2;
            $this->pagination->initialize($config);
            $this->data['pagination'] = $this->pagination->create_links();
            $offset = $this->uri->segment(2);
        }
        else {
            $this->data['pagination'] = '';
            $offset = 0;
        }

        // Fetch articles
        $this->db->where('pubdate <=', date('Y-m-d'));
        $this->db->limit($perpage, $offset);
        $this->data['articles'] = $this->article_m->get();
    }   

}

希望你能帮帮我 谢谢

你好像没有

function index()

在您的管理控制器中。

我没有看到任何路线,也没有 URL 指向您的 Admin_Controller 。如果用户键入 http://.../admin/page,Codeigniter 将尝试查找名为 Admin 而不是 Admin_controller 的控制器,除非您设置了路由。

此外,你有这条路线:$route[':any'] = "page/index/";这条路线将接受任何给定的 URL 并将用户重定向到 Page 控制器(你没有提供任何代码) .我会去掉它,或者它的作用是什么?

您需要决定的是您的管理员 URL 是否应该是这样的:http://www.my_site.com/admin/page 或者这个:http://www.my_site.com/admin_controller/page

我个人会选择第一个,因为它看起来更干净。这意味着你必须决定你的控制器是应该保持命名为 Admin_Controller 还是只命名为 Admin。我会选择 Admin 这样你就不必处理路线了。


最后的结果应该是这样的:

您的管理员URL:http://www.my_site.com/admin/page

您的控制器:application/controllers/Admin.php

class Admin extends MY_Controller {

    public function __construct() {
        //... your code here
    }

    public function page() {
        //... your code here
    }
}