Tim C
Tim C

Reputation: 5714

CodeIgniter Route found but gives 404

Ive been battling with codeigniter routes for days. and it has seriously held back my productivity. Thus, any help here would be very much appreciated.

I have a login form:

Im using codeigniter route debugger script, to check and debug my routes.

When I check my routing logs the following shows, for a correct login attempt.

DEBUG - 2018-05-11 03:40:10 --> Client sent : users/welcome

DEBUG - 2018-05-11 03:40:10 --> Route found : users/welcome --> users/welcome

DEBUG - 2018-05-11 03:40:10 --> Redirecting to : users/welcome -->users/welcome

users/welcome DEBUG - 2018-05-11 03:40:10 --> Global POST, GET and

COOKIE data sanitized ERROR - 2018-05-11 03:40:10 --> 404 Page Not

As you can see the route is found and then it redirects to the route BUT then somehow it results in a 404 page not found.

However I clearly have a welcome page in my views as can be seen on image below:

enter image description here

I stripped everything and tried to make my routes as simple as possible. config/routes.php reads as follow.

$route['users']= 'users';
$route['users/index'] = 'users/index';
$route['users/login'] ='users/login';
$route['users/welcome'] ='users/welcome';
$route['users/failed'] ='users/failed';

$route['(:any)'] = 'pages/index/$1';
$route['default_controller'] = 'pages/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Page URL reads as follow: http://mysite/users/welcome = 404 error as described above.

Any help appreciated.

Additional Info: Controller Users()

public function login(){ $data['title'] = 'Sign In';

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

if($this->form_validation->run() === FALSE){
    $this->load->view('templates/header');
    $this->load->view('users/login', $data);
    $this->load->view('templates/footer');
} else {

    // Get username
   echo $username = $this->input->post('username');
    // disabled encryption for testing
   echo $password = $this->input->post('password');

    // Login user
    $user_id = $this->users_model->login($username, $password);

    if($user_id){
        // Create session
        $user_data = array(
            'userID' => $user_id,
            'username' => $username,
            'logged_in' => true
        );

        $this->session->set_userdata($user_data);

        // Set message
        $this->session->set_flashdata('user_loggedin', 'You are now logged in');

            redirect('users/welcome');
    } else {
        // Set message
        $this->session->set_flashdata('login_failed', 'Login is invalid');
            redirect('users/failed');

    }
}

Upvotes: 2

Views: 1119

Answers (1)

Friderich Weber
Friderich Weber

Reputation: 290

Each route maps to a controller, not a view.

You don't need to define your routes when they map specifically to the controller class and method. IE: http://website.com/users/login "magically" maps to Users Controller login method.

You only need to define routes when you get creative.

So here's the short of your Users class (controller)

Routes

// You don't need these for your current usage
$route['users']= 'users';
$route['users/index'] = 'users/index';
$route['users/login'] ='users/login';
$route['users/welcome'] ='users/welcome';
$route['users/failed'] ='users/failed';

// keep these below.
$route['(:any)'] = 'pages/index/$1'; //<-- this will give you problems probably
$route['default_controller'] = 'pages/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

As a note about routing. IF you wanted to use example.com/login that is when you would map the route to the controller/method thusly: $route['login'] ='users/login';

Users Controller

class Users extends CI_Controller
{

    // routes to example.com/users/
    public function index() 
    {
       redirect('users/login');
    }     

     // routes to example.com/users/login
     public function login() {
         $data['title'] = 'Sign In';

         $this->form_validation->set_rules('username', 'Username', 'required');
         $this->form_validation->set_rules('password', 'Password', 'required');

         if($this->form_validation->run() === FALSE){
             $this->load->view('templates/header');
             $this->load->view('users/login', $data);
             $this->load->view('templates/footer');
         } else {

             // Get username
             echo $username = $this->input->post('username');
             // disabled encryption for testing
             echo $password = $this->input->post('password');

             // Login user
             $user_id = $this->users_model->login($username, $password);

             if($user_id){
                 // Create session
                 $user_data = array(
                     'userID' => $user_id,
                     'username' => $username,
                     'logged_in' => true
                 );

                 $this->session->set_userdata($user_data);

                 // Set message
                 $this->session->set_flashdata('user_loggedin', 'You are now logged in');

                 redirect('users/welcome');
               } else {
                   // Set message
                   $this->session->set_flashdata('login_failed', 'Login is invalid');
                   redirect('users/failed');

                }
         }
    }

    // routes to example.com/users/welcome
    public function welcome() {
        $this->load->view('users/welcome');
    }

    // routes to example.com/users/failed
    public function failed() {
       $this->load->view('users/failed');
    }

}

Upvotes: 2

Related Questions