dpt1712 2011-11-08 14:23
浏览 55
已采纳

使用CodeIgniter进行路由,使用正确的$ routes获取404

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.

  • 写回答

3条回答 默认 最新

  • drexlz0623 2011-11-08 14:51
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)