Codeigniter管理路线

时间:2015-08-07 13:44:51

标签: php codeigniter codeigniter-url codeigniter-routing

我遇到了这个问题。 尝试访问codeigniter中的管理页面时出现404错误,我只能访问前端页面。 在这里你有我的 route.php 文件:

$route['default_controller'] = "page";
$route['404_override'] = '';
$route[':any'] = "page/index/$1"; **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/$1"; **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

拜托,有人可以帮帮我吗? 提前谢谢。

伙计感谢您的回复 在这里你有我的页面管理控制器,我也修改了上面的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();
    }   

}

希望你能帮帮我 感谢

2 个答案:

答案 0 :(得分:2)

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

此外,您有此路由:$route[':any'] = "page/index/$1";此路由将获取给定的任何URL并将用户重定向到Page控制器(您尚未提供任何代码)。我会摆脱它,或它的功能是什么?

您需要决定的是,您的管理网址是否应为:http://www.my_site.com/admin/page 或者:http://www.my_site.com/admin_controller/page

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

最终结果应为:

您的管理网址: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
    }
}

答案 1 :(得分:-1)

你似乎没有

function index()

在您的管理员控制器中。