Thanks!
I moved files to "application/libraries/" (not inside of any subdirectory) and it works, but I want to make some tests.
I copied methods users_post and users_get to my control :
Code:
<?php
require_once("application/libraries/REST_Controller.php");
class Users extends REST_Controller
{
function __construct()
{
// Construct the parent class
parent::__construct();
// Configure limits on our controller methods
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
$this->methods['user_get']['limit'] = 500; // 500 requests per hour per user/key
$this->methods['user_post']['limit'] = 100; // 100 requests per hour per user/key
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
}
public function users_get()
{
// Users from a data store e.g. database
$users = [
['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
];
$id = $this->get('id');
// 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['id']) && $value['id'] === $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
}
}
public function users_post()
{
// $this->some_model->update_user( ... );
$message = [
'id' => 100, // Automatically generated by the model
'name' => $this->post('name'),
'email' => $this->post('email'),
'message' => 'Added a resource'
];
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
But running url "http://local-ci3.com/api/users" in browser I got
Code:
{"status":false,"error":"Unknown method"}
response. Method users_get was not found! why?
I made JavaScript sample to test POST action :
Code:
function new_user() {
request_url = '{$base_url}api/user'
$("#div_request_url").html("POST : " + request_url);
var user_is_active = ''
if ($("#user_is_active_y").is(':checked')) {
user_is_active = 'Y'
}
if ($("#user_is_active_n").is(':checked')) {
user_is_active = 'N'
}
alert( "new_user request_url ::"+var_dump(request_url) ) // shows http://local-ci3.com/api/user
$.ajax({
url: request_url,
dataType: "json",
type: "POST",
data: {
"name": $("#user_name").val(),
"is_active": user_is_active,
"activation_key": $("#user_activation_key").val(),
"login": $("#user_login").val(),
"password": $("#user_password").val(),
"active_till_date": $("#user_active_till_date").val(),
},
beforeSend: function (xhr) {
//xhr.setRequestHeader("X_REST_USERNAME", "<?php echo Yii::app()->params['RESTusername'] ?>");
//xhr.setRequestHeader("X_REST_PASSWORD", "<?php echo Yii::app()->params['RESTpassword'] ?>");
console.log(xhr);
},
success: function (data, textStatus, XMLHttpRequest) {
But in console of browser I saw :
Code:
POST http://local-ci3.com/api/user 404 (Not Found)
Which is the correct way? Have I to set some changes in application/config/routes.php or .htaccess files?