【问题标题】:Code Igniter RESTful Modeling and ControllerCodeigniter RESTful 建模和控制器
【发布时间】:2013-07-27 10:25:26
【问题描述】:

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

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

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

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

使用我的逻辑如下:

控制器: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 有问题吗?

【问题讨论】:

  • 看起来您的路由器没有路由到正确的方法
  • 如何修复它然后@user1281385?我正在使用 codeigniter 默认路由功能。
  • 数据库中有没有导致id的错误?

标签: php mysql codeigniter codeigniter-2


【解决方案1】:

插入控制器:

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

试试这条路线:

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

【讨论】:

    【解决方案2】:

    我认为您访问的 url 是错误的。尝试使用 url @http://localhost/&lt;project&gt;/index.php/user/&lt;method_name&gt;/&lt;user_id&gt; 访问

    【讨论】:

    • 所以例如 /index.php/user_get/ 你不接受函数中的任何参数,所以我不太确定。您可能需要进一步研究控制器的工作原理。一旦你研究它们,它们就是一个相当简单的概念。
    • 我尝试使用以下方法访问网址:/user/user_get/1 和 /user_get/1 但两者都显示相同的结果。
    • 确保通过 get 放置变量!
    • User_get 在用户被 get 请求时被访问。使用 post 访问时的 User_post 例如
    • 你是什么意思让var通过get?从浏览器参数?或重构控制器?顺便说一下,我在 url 中添加了参数,因为我是这个框架的新手
    【解决方案3】:

    另一种方法是使用 URI 类。

    而不是编写以下内容:

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

    改为:

    $userid = $this->uri->segment(3);
    
    Would be 3 based off the url being http://localhost/<project>/user/<method_name>/<user_id>
    

    URI 类参考:http://www.codeigniter.com/user_guide/libraries/uri.html

    为了使用 $this->get();必须有一个查询参数才能获取。示例:id=

    如果您传递查询参数,则可以将这些值存储在数组中,如下所示。

    if(!empty($this->get()))
    {
        $params[] = $this->get();
    }
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      您应该尝试使用 __construct() 函数将模型文件包含到控制器文件中,如下所示:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多