php - Codeigniter RESTful 建模和 Controller

标签 php mysql codeigniter codeigniter-2

我使用非框架进行开发已经大约 3 年了,我听说过 PHP 框架 codeigniter。

我想我错过了使用这个框架的一些东西,该框架用于我的移动应用程序的 API。

我在使用 Phil 的 CI 框架、RESTful 插件从数据库获取数据时遇到了一些问题。我的浏览器显示错误代码:

 {"status":false,"error":"Unknown method."}

使用下面的逻辑:

Controller :user.php

  <?php

  require(APPPATH.'libraries/REST_Controller.php');

  class User extends REST_Controller{

  public function user_get()  
  {  
    //IF USER NOT EXIST
    if(!$this->get('id'))
    {
        $this->response(NULL, 400);
    }

    //CHECK TO MODEL FUNCTION
    $user = $this->user_model->get_user($this->get('id'));

    //IF USER EXIST
    if($user)
    {
        $this->response($user, 200); // 200 being the HTTP response code
    }
}  

   public function user_put()  
   {  
    // create a new user and respond with a status/errors  
   }  

   public function user_post()  
   {  
    // update an existing user and respond with a status/errors  
   }  

   public function user_delete()  
   {  
    // delete a user and respond with a status/errors  
   }  
 }
?>

模型:user_model.php

 <?php

    class User_model extends CI_Model{

    function __construct() {
        parent::__construct();        
    }

    function get_user($id){
        $query = $this->db->get('mt_user', array('idmt_user'=>$id));
        return $query->row_array();
    }

   }
   ?>

我正在访问包含以下行的数据库:

idmt_user,username,password, emails, createdate

使用 mozilla 访问:

@http://localhost/<project>/index.php/user/<userid>

错误在哪里?

谢谢。

更新, 我已经定义了数据库的自动加载。但这个问题仍然存在。有什么帮助吗?谢谢

正如下面的回答所述,我尝试访问网址/user/get_user/但仍然显示相同的结果。在数据库中使用idmt_user有问题吗?

最佳答案

插入 Controller :

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

尝试这条路线:

http://localhost//index.php/user/user/id/

关于php - Codeigniter RESTful 建模和 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896719/

相关文章:

php - 自定义帖子类型 'single-custom.php' 下一个和上一个链接卡在循环中

php - SELECT DISTINCT 返回所有值

javascript - 如何使用一个id将多个值传递到模式中

PHP 框架 - 布局动态菜单

php - 如何将数据库结果集合组织为层次结构?

PHP hash_hmac() 操作系统差异

php - 在 PHP 中获取对象的内存大小?

mysql - 复合关键索引是否可以提高 or 子句的性能

php - 数据库关系

php - 使用 codeigniter PHP fatal error : Class 'MY_Model' not found