Reputation: 131
I'm currently studying rest api and i am using a ci framework with chris's rest server and i am having a problem with my users_get when i put the id in my links for example this,"http://localhost/api/example/users/id/1" it still shows all the data from the database, is my query wrong? here.
COntroller
public function users_get()
{
// Users from a data store e.g. database
$users =
$result=$this->regusers->get();
$id = $this->get('result');
// 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($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 as $key => $value)
{
if (isset($value['result']) && $value['result'] === $id)
{
$user = $value;
}
}
}
if (!empty($user))
{
$this->set_response($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
<?php
class Regusers Extends CI_Model{
public function get(){
$query=$this->db->get('tbl_user');
return $query->result();
}
}
View
<li><a href="<?php echo site_url('api/example/users'); ?>">Users</a> - defaulting to JSON</li>
<li><a href="<?php echo site_url('api/example/users/format/csv'); ?>">Users</a> - get it in CSV</li>
<li><a href="<?php echo site_url('api/example/users/id/1'); ?>">User #1</a> - defaulting to JSON (users/id/1)</li>
<li><a href="<?php echo site_url('api/example/users/1'); ?>">User #1</a> - defaulting to JSON (users/1)</li>
<li><a href="<?php echo site_url('api/example/users/id/1.xml'); ?>">User #1</a> - get it in XML (users/id/1.xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1/format/xml'); ?>">User #1</a> - get it in XML (users/id/1/format/xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1?format=xml'); ?>">User #1</a> - get it in XML (users/id/1?format=xml)</li>
<li><a href="<?php echo site_url('api/example/users/1.xml'); ?>">User #1</a> - get it in XML (users/1.xml)</li>
<li><a id="ajax" href="<?php echo site_url('api/example/users/format/json'); ?>">Users</a> - get it in JSON (AJAX request)</li>
<li><a href="<?php echo site_url('api/example/users.html'); ?>">Users</a> - get it in HTML (users.html)</li>
<li><a href="<?php echo site_url('api/example/users/format/html'); ?>">Users</a> - get it in HTML (users/format/html)</li>
<li><a href="<?php echo site_url('api/example/users?format=html'); ?>">Users</a> - get it in HTML (users?format=html)</li>
Upvotes: 0
Views: 1637
Reputation: 36
The controller will always return all users because $id
is always FALSE
or null
due to the fact that there is no 'result' parameter in the the get request which you used to assign to the $id
variable.
I think the solution would be to change the URL to the following format api/example/users?id=1
then in controller use $id= $this->input->get("id");
to assign it to the variable.
Upvotes: 1