0

Im having an issue on my CI code. I cant access the authenticate method of my Login controller. There's a 404 error appearing in my browser window. 404 Error

Here's my code

 <!--This will serve as the form -->
    <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="">Brand</a>
        </div>       
        <form role="form" class="navbar-form navbar-right login-form">
            <div class="form-group">
                <input type="text" placeholder="Username" class="form-control"  name="username">
                <input type="password" placeholder="Password" class="form-control" name="password">
            </div>
            <input type="submit" class="btn-default btn" value="Login" id="btn-login">
        </form>
      </div><!-- /.container-fluid -->
      </nav>

This is the Login Controller with the authenticate() method.

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

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

    public function index() {
        $this->load->view('login');
        $this->template->set_layout('default');
        $this->template->set_partial('nav', 'partials/nav');
        $this->template->append_metadata('<script src="' . base_url("js/script.js") . '"></script>');
        $this->template->build('login');    
    }

    public function authenticate() {
        $error=0;
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if ($this->user->login($username, $password)) {
            $error=0;
            echo json_encode(array('status' => 'success', 'error' => $error));
        } else{
            $error=1;
            echo json_encode(array('status' => 'error', 'error' => $error));
        }

        echo json_encode(array('status' => 'success', 'error' => $error));
    }

}

Im calling the controller using AJAX with the url: "login/authenticate" and the default controller is set to Login. I have no problem with the model. I'm sure that I have configured it properly. Thank You.

Tibs
  • 735
  • 5
  • 16
  • 1
    you should check/share your routes – julian soro Dec 11 '14 at 06:28
  • $route['default_controller'] = "login"; this is my route. – Tibs Dec 11 '14 at 06:30
  • 1
    `http://localhost/hci/login/authenticate` hit this link at your browser and let me know is it showing page not found error? – Shaiful Islam Dec 11 '14 at 06:34
  • I've tried it. It returns Object Not Found 404 Error – Tibs Dec 11 '14 at 06:40
  • When I try to echo something in the index() of Login Controller, it will display on the page. I tried echo "This will appear";. And it will appear above the nav bar. – Tibs Dec 11 '14 at 06:45
  • 1
    have you set your `.htaccess` ? if no try this in browser `http://localhost/hci/index.php/login/authenticate`. if yes post it. – Karan Thakkar Dec 11 '14 at 06:47
  • I have not tried setting my .htaccess. There's a json response when I tried this link http://localhost/hci/index.php/login/authenticate. Where is the directory of .htaccess? – Tibs Dec 11 '14 at 06:53
  • Im sorry Im new to CI. Im only self studying this framework with the help of online tutorials, blogs and articles. I'm stuck on this problem since last night. – Tibs Dec 11 '14 at 06:56
  • 1
    `.htaccess` should be in the root of CI installation (in your case hci directory) you can read about `Removing index.php in Codeigniter` which is the issue in your case. [link1](http://stackoverflow.com/questions/1445385/how-to-remove-index-php-in-codeigniters-path) [link2](http://stackoverflow.com/questions/19183311/codeigniter-removing-index-php-from-url) OR here in [documentation](https://ellislab.com/codeigniter/user-guide/general/urls.html) – Karan Thakkar Dec 11 '14 at 06:57
  • glad to help @Tibs , i hope the links are helpful. – Karan Thakkar Dec 11 '14 at 07:02

0 Answers0