为什么我的所有页面都转到CodeIgniter中的同一个控制器?

时间:2016-05-26 15:02:36

标签: php codeigniter

当我加载我的主页时,它完美无缺。我为CSS和JS文件制作的自定义帮助程序正在运行,它通过正确的控制器/视图加载。

然而,每个其他页面都从错误的控制器(新闻控制器)加载。并且控制器无论出于何种原因都不使用自定义助手,因此页面上没有样式。

这是我在routes.php和我的每个控制器中的内容:

route.php

$route['translate_uri_dashes'] = FALSE;

$route['news'] = 'news/index/1';
$route['news/create'] = 'news/create';
$route['news/view'] = 'news/index/1';
$route['news/view/(:any)'] = 'news/view/$1';
$route['news/(:any)'] = 'news/index/$1';

$route['logout'] = 'login/logout';
$route['register'] = 'login/register';
$route['confirm/(:any)/(:any)'] = 'login/confirm/$1/$2';

$route['default_controller'] = 'index';
$route['404_override'] = 'error';

索引控制器

<?php

class Index extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->library('session');

        $data['page'] = 'Home';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            $data['user'] = $this->user_model->get_user($_SESSION['id']);
        }

        $this->load->view('layout/header', $data);
        $this->load->view('layout/breadcrumb', $data);
        $this->load->view('layout/body');
        $this->load->view('index');
        $this->load->view('layout/close');
        $this->load->view('layout/footer');
    }
} 

新闻控制器

<?php

class News extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->library('session');
    }

    public function index($page) {
        $data['page'] = 'News';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            $data['user'] = $this->user_model->get_user($_SESSION['id']);
        }

        $data['news'] = $this->news_model->get_news(NULL, $page);
        $data['pageNo'] = $page;
        $data['entries'] = $this->news_model->get_total();

        $this->load->view('layout/header', $data);
        $this->load->view('layout/breadcrumb', $data);
        $this->load->view('layout/body');
        $this->load->view('news/index', $data);
        $this->load->view('layout/close');
        $this->load->view('layout/footer');
    }

    public function view($id) {
        $data['page'] = 'News';

        if (isset($_SESSION['id'])) {
            $data['user'] = $this->user_model->get_user($_SESSION['id']);
        }

        $data['news'] = $this->news_model->get_news($id);

        $data['section'] = $data['news']['title'];

        $data['prev'] = $this->news_model->get_prev($data['news']['pubDate']);
        $data['next'] = $this->news_model->get_next($data['news']['pubDate']);

        $data['pub'] = $this->user_model->get_user($data['news']['publisher']);

        $this->load->view('layout/header', $data);
        $this->load->view('layout/breadcrumb', $data);
        $this->load->view('layout/body');
        $this->load->view('news/view', $data);
        $this->load->view('layout/close');
        $this->load->view('layout/footer');
    }

    public function create() {
        $this->news_model->set_news();
        $this->session->set_userdata('created', 1);
        redirect('news');
    }
} 

登录控制器

<?php

class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
    }

    public function index() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['page'] = 'Login';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            redirect('/');
        } else {
            $this->form_validation->set_rules('username', 'Username', 'required', array('required' => 'You must provide a %s.'));
            $this->form_validation->set_rules('password', 'Password', 'required', array('required' => 'You must provide a %s.'));

            if ($this->form_validation->run() !== FALSE) {
                $user = $this->user_model->login();
                if ($user) {
                    $this->session->set_userdata('id', $user['id']);
                    redirect('/');
                } else {
                    $this->session->set_userdata('loginerr', 1);
                }
            }

            $this->load->view('layout/header', $data);
            $this->load->view('layout/breadcrumb', $data);
            $this->load->view('layout/body');
            $this->load->view('login');
            $this->load->view('layout/close');
            $this->load->view('layout/footer');
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('/');
    }

    public function register() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['page'] = 'Register';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            redirect('/');
        } else {
            $this->form_validation->set_rules('username', 'Username', 'required', array('required' => 'You must provide a %s.'));
            $this->form_validation->set_rules('email', 'Email', 'required', array('required' => 'You must provide a %s.'));
            $this->form_validation->set_rules('password', 'Password', 'required', array('required' => 'You must provide a %s.'));
            $this->form_validation->set_rules('confpass', 'Confirm Password', 'required', array('required' => 'You must provide a %s.'));

            if ($this->form_validation->run() !== FALSE) {
                $this->user_model->register();

                if (isset($_SESSION['regerror'])) {
                    if ($_SESSION['regerror'] == 1) {
                        $data['error'] = 'Username is already in use.';
                    } else if ($_SESSION['regerror'] == 2) {
                        $data['error'] = 'Email Address is already in use.';
                    } else if ($_SESSION['regerror'] == 3) {
                        $data['error'] = 'Passwords do not match';
                    } else {
                        $data['error'] = 'This IP Address has been banned for the following reason: ' . $_SESSION['regerror'];
                    }
                    unset($_SESSION['regerror']);
                } else if (isset($_SESSION['register'])) {
                    $data['success'] = 1;
                    unset($_SESSION['register']);
                }
            }

            $this->load->view('layout/header', $data);
            $this->load->view('layout/breadcrumb', $data);
            $this->load->view('layout/body');
            $this->load->view('register', $data);
            $this->load->view('layout/close');
            $this->load->view('layout/footer');
        }
    }

    public function confirm($un = NULL, $conf = NULL) {
        $data['page'] = 'Confirmation';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            redirect('/');
        } else {
            if ($un == NULL || $conf == NULL) {
                $data['message'] = 'No account selected...';
            } else {
                $check = $this->user_model->confirm($un, $conf);
                if ($check == 1) {
                    $data['message'] = 'Invalid information provided...';
                } else if ($check == 2) {
                    $data['message'] = 'User already confirmed...';
                } else {
                    $data['message'] = 'Your account has been activated! You may now <a href="' . base_url() . 'login">Login</a>!';
                }
            }

            $this->load->view('layout/header', $data);
            $this->load->view('layout/breadcrumb', $data);
            $this->load->view('layout/body');
            $this->load->view('confirm', $data);
            $this->load->view('layout/close');
            $this->load->view('layout/footer');
        }
    }
} 

错误控制器

<?php

class Error extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
    }

    public function index() {
        $data['page'] = 'Error 404';
        $data['section'] = 'None';

        if (isset($_SESSION['id'])) {
            $data['user'] = $this->user_model->get_user($_SESSION['id']);
        }

        $this->output->set_status_header('404');

        $this->load->view('layout/header', $data);
        $this->load->view('layout/breadcrumb', $data);
        $this->load->view('layout/body');
        $this->load->view('404');
        $this->load->view('layout/close');
        $this->load->view('layout/footer');
    }
}

谁能告诉我为什么会这样?

1 个答案:

答案 0 :(得分:1)

['default_controller']应该始终位于底部,这是路线的最后接触点。

任何自定义路线都应位于其上方,

news/(:any)将覆盖其他新闻,它应低于news/viewnews/create

e.g:

$route['translate_uri_dashes'] = FALSE;

$route['news'] = 'news/index/1';
$route['news/create'] = 'news/create';
$route['news/view'] = 'news/index/1';
$route['news/view/(:any)'] = 'news/view/$1';
$route['news/(:any)'] = 'news/index/$1';

$route['logout'] = 'login/logout';
$route['register'] = 'login/register';
$route['confirm/(:any)/(:any)'] = 'login/confirm/$1/$2';

$route['default_controller'] = 'index';
$route['404_override'] = 'error';