我正在编写教程Working with RESTful Services in CodeIgniter,我遇到一行代码问题。
在user_get()
函数条件if(!$this->get('id'))
如果我尝试获取资源http://localhostApp/api/user/1
我错过了什么?
我是否以错误的方式请求资源?
这是我的Api控制器:
class Api extends REST_Controller
{
function user_get(){
if(!$this->get('id')){ //Problem line
$this->response(NULL, 400);
}
$user = $this->user_model->get( $this->get('id') );
if($user){
$this->response($user, 200); // 200 being the HTTP response code
}else{
$this->response(NULL, 404);
}
}
function user_post(){
$result = $this->user_model->update( $this->post('id'), array(
'name' => $this->post('name'),
'email' => $this->post('email')
));
if($result === FALSE){
$this->response(array('status' => 'failed'));
}else{
$this->response(array('status' => 'success'));
}
}
function users_get(){
$users = $this->user_model->get_all();
if($users){
$this->response($users, 200);
}else{
$this->response(NULL, 404);
}
}
}
?>
如果您需要任何其他信息,请告知我们 提前谢谢
答案 0 :(得分:2)
如果您想将数据传递到SELECT * FROM master WHERE valueFR LIKE 'ja';
,则必须在网址中对其进行定义。
user_get()
在此处定义http://localhostApp/api/user/1
。但是没有传递任何id来调用哪个用户
所以你的网址应该是
user
注意:不要只是将该网站的代码复制并粘贴到您的项目中。根据需要更改
答案 1 :(得分:1)
这就是我使用RESTful服务的方式 - 使用Phil Sturgeon Rest Server。
使用POST代替GET来检索用户详细信息
routes.php
$route['api/view/user_details'] = 'api/view/user_details';
测试表格
<form class="form-horizontal inline" action="<?php echo base_url(); ?>api/view/user_details" method="post">
<label for="user_id">Enter User ID</label>
<input id="user_id" type="text" name="id" value="" />
<input type="submit" name="submit" value="submit" />
</form>
用于在JSON中返回用户信息的控制器方法
public function user_details_post()
{
$id = $this->post('id');
$this->load->model('admin_model');
$users = $this->admin_model->view_clients();
// If the id parameter doesn't exist return all the users
if ($id == NULL)
{
// Check if the users data store contains users (in case the database result returns NULL)
if ($users)
{
// Set the response and exit
$this->response(['all_users'=>$users], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
// Find and return a single record for a particular user.
$id = (int) $id;
// Validate the id.
if ($id <= 0)
{
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// Get the user from the array, using the id as key for retreival.
// Usually a model is to be used for this.
$user = NULL;
if (!empty($users))
{
foreach ($users['user_details'] as $key => $value)
{
if (isset($value['id']) && $value['id'] === $id)
{
$user = $value;
}
}
}
if (!empty($user))
{
$this->set_response([
'user_details'=>$user], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
$this->set_response([
'status' => FALSE,
'message' => 'User could not be found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
MODEL:将包含所有详细信息的用户数组返回给控制器
public function view_clients()
{
$result=$this->db->get('clients');
$results=array();
foreach ($result->result() as $row)
{
$id = $row->id;
$id = (int) $id;
$results[] = array(
'id' => $id,
'full_name' => $row->full_name,
'gender' => $row->gender,
'email' => $row->email,
'dob' => $row->dob,
'phone' => $row->phone,
'address' => $row->address,
'username' => $row->username,
'country' => $row->country,
'city' => $row->city,
'rec_status' => $row->rec_status,
'crt_date' => $row->crt_date,
'email_ver_status' => $row->email_ver_status,
'mobile_ver_status' => $row->mobile_ver_status,
);
$return_array= array("status:" =>'success',
"user_details" => $results );
}
return($return_array);
}
响应为ID为
的POST请求的JSON{
"user_details": {
"id": 1,
"full_name": "sudheesh sudhakaran",
"gender": "Male",
"email": "mails2sudheesh@gmail.com",
"dob": "12\/03\/1986",
"phone": "0567462013",
"address": "kottayil house po kundaliyoor\r\nthrissur",
"username": "sudheesh",
"country": "India",
"city": "KERALA",
"rec_status": "A",
"crt_date": "",
"email_ver_status": null,
"mobile_ver_status": null
}
}
要获取所有用户的记录,请POST一个NULL值。
创建此问题时提出的问题是here