Enrutamiento con CodeIgniter, obteniendo 404 con $ rutas correctas

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.

preguntado el 08 de noviembre de 11 a las 14:11

3 Respuestas

Sounds to me like your .htaccess isn't set up or being read.

Not all default server configurations will go up the path until they find your index.php script.

respondido 08 nov., 11:18

Well, thank you. But what sort of .htaccess are you speaking of ? I need to create one inside the root folder ? So routing in CI doesn't work by default ? - turbodoom

@turbodoom, this really has nothing to do with Code Igniter. Your web server won't hit index.php unless it has been instructed to do so. You can either do this in your Apache config, or .htaccess. If you're hosting on IIS, it's easiest with IIS 7, and you will need to setup web.config. - Brad

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

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

a

$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

respondido 08 nov., 11:18

That was it, I found the answer a few minutes before you but thanks mate ! - turbodoom

Not directly related, but you should have a look at the Guía de estilo de CodeIgniter

Method names should be lowercased and words separated by underscores ('_'). You might not like it and find it a pain in the -you know where-, but trust me sticking with the standards is always the best idea, plus it looks nice and consistent with third party libraries/plugins/helpers.

Plus you don't want to have to custom route cada método de cada controller. And you also don't want uppercase characters in your urls

respondido 08 nov., 11:22

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.