Search code examples
phpcodeignitercodeigniter-routing

Routing with CodeIgniter, getting 404 with correct $routes


Using CodeIgniter, I am getting a strange behaviour in my code. What I want is to have a sort of person listing in my database and when I type the id of a given person, a page appears with all the informations we’ve got about this person in the database.

Just a simple thing. I succeeded with the news official tutorial thing and it doesn’t work at all with this !

I wrote a controller inherited class which is named Person, with a viewPersonById method, just like that :

class Person extends CI_Controller{

      public function __construct()
      {
      parent::__construct();
      $this->load->model('Person_Model');
}

public function index()
{

}

public function viewPersonById($parId){
    $data['person'] = $this->Person_Model->get($parId);

    $data['title'] = 'Person information';

    $this->load->view('templates/header', $data);
    $this->load->view('people/view', $data);
    $this->load->view('templates/footer');
} 

My routes.php is written like this :

$route['people/(:any)'] = 'Person/viewPersonById/$1';

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

$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view'; 

And all that I get is a 404 when I enter http://127.0.0.1:8888/ci/index.php/people/1 in my browser. What is wrong in my code ? I can’t see it.

Any ideas ? Thanks and sorry for my poor english.

EDIT :

Ok guys, I found out what was wrong. I feel like the biggest jackass ever because the name of my files were weird like "person.controler.php" and "person.model.php". The name of your controller and what you put in routes.php have to match exactly.

So I just had to rename person.controler.php to person.php and person.model.php to person_model.php so the model can be loaded within the controler. CI uses the names of the files to see what it has to load. Be careful with that.


Solution

  • If your controller filename if person.php, then you should change:

    $route['people/(:any)'] = 'Person/viewPersonById/$1';

    to

    $route['people/(:any)'] = 'person/viewPersonById/$1';

    lowercase p in person.

    PS: Try to open http://127.0.0.1:8888/ci/index.php/Person/viewPersonById/1, you'll know the issue