简体   繁体   中英

Why do all my pages go to the same controller in CodeIgniter?

When I load up my homepage, it works perfectly. The custom helper I made for the CSS and JS files is working and it loads through the correct controller/view.

Every other page, however, loads from a the wrong controller (the News Controller). And the controller isn't using the custom helper for whatever reason, so there's no style on the page.

Here's what I have in my routes.php and each of my controllers:

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';

Index Controller

<?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');
    }
} 

News Controller

<?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');
    }
} 

Login Controller

<?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');
        }
    }
} 

Errors Controller

<?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');
    }
}

Can anyone tell me why this is happening?

['default_controller'] should ALWAYS be at the bottom, this is the LAST point of contact for the routes.

Any custom routes should be above it, also

news/(:any) will overwrite the other news ones, it should be below news/view and news/create

eg:

$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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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