From 7c1c7ba64d45f024f8b85655b0c29cbda9964cac Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 16:45:04 +0300 Subject: [PATCH 01/17] A variable name fix: $timelimit -> $time_limit --- application/libraries/REST_Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/libraries/REST_Controller.php b/application/libraries/REST_Controller.php index ebbc64b8..74f6491f 100644 --- a/application/libraries/REST_Controller.php +++ b/application/libraries/REST_Controller.php @@ -1101,7 +1101,7 @@ protected function _check_limit($controller_method) // How many times can you get to this method in a defined time_limit (default: 1 hour)? $limit = $this->methods[$limited_method_name]['limit']; - $timelimit = (isset($this->methods[$limited_method_name]['time']) ? $this->methods[$limited_method_name]['time'] : 3600); // 3600 = 60 * 60 + $time_limit = (isset($this->methods[$limited_method_name]['time']) ? $this->methods[$limited_method_name]['time'] : 3600); // 3600 = 60 * 60 // Get data about a keys' usage and limit to one row $result = $this->rest->db From 1a4fe2765ed2b6fbafdbfc23ff8d29976ce87f94 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 16:51:00 +0300 Subject: [PATCH 02/17] Format library: Suppressing the "array to string conversion" notice. --- application/libraries/Format.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/application/libraries/Format.php b/application/libraries/Format.php index e008c170..6615e2cf 100644 --- a/application/libraries/Format.php +++ b/application/libraries/Format.php @@ -289,6 +289,10 @@ public function to_html($data = NULL) foreach ($data as $row) { + // Suppressing the "array to string conversion" notice. + // Keep the "evil" @ here. + $row = @ array_map('strval', $row); + $this->_ci->table->add_row($row); } @@ -364,6 +368,10 @@ public function to_csv($data = NULL, $delimiter = ',', $enclosure = '"') break; } + // Suppressing the "array to string conversion" notice. + // Keep the "evil" @ here. + $record = @ array_map('strval', $record); + // Returns the length of the string written or FALSE fputcsv($handle, $record, $delimiter, $enclosure); } From fa147a62f1f41a2e251464e89c31c7a9c44291da Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 16:53:59 +0300 Subject: [PATCH 03/17] REST_Controller: Whitespace tunning. --- application/libraries/REST_Controller.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/libraries/REST_Controller.php b/application/libraries/REST_Controller.php index 74f6491f..d73176a7 100644 --- a/application/libraries/REST_Controller.php +++ b/application/libraries/REST_Controller.php @@ -180,7 +180,7 @@ abstract class REST_Controller extends CI_Controller { */ protected $request = NULL; - /** + /** * Contains details about the response * Fields: format, lang * Note: This is a dynamic object (stdClass) @@ -2106,10 +2106,10 @@ protected function _check_access() // Query the access table and get the number of results return $this->rest->db - ->where('key', $this->rest->key) - ->where('controller', $controller) - ->get($this->config->item('rest_access_table')) - ->num_rows() > 0; + ->where('key', $this->rest->key) + ->where('controller', $controller) + ->get($this->config->item('rest_access_table')) + ->num_rows() > 0; } } From 1c7aec58b7e6692bfd6fa020be9e5a41d6eaafc7 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 16:59:19 +0300 Subject: [PATCH 04/17] REST_Controller: Removing a bad fragment of code of mine, the idea was wrong. --- application/libraries/REST_Controller.php | 25 +++++------------------ 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/application/libraries/REST_Controller.php b/application/libraries/REST_Controller.php index d73176a7..bf8d9362 100644 --- a/application/libraries/REST_Controller.php +++ b/application/libraries/REST_Controller.php @@ -816,29 +816,14 @@ protected function _detect_output_format() return $matches[1]; } - if (empty($this->_get_args) === FALSE) + // Get the format parameter named as 'format' + if (isset($this->_get_args['format'])) { - // Get the format parameter named as 'format' - if (isset($this->_get_args['format']) === TRUE) - { - $format = strtolower($this->_get_args['format']); - - if (isset($this->_supported_formats[$format]) === TRUE) - { - return $format; - } - } + $format = strtolower($this->_get_args['format']); - // A special case: users/1.json - elseif (count($this->_get_args) === 1 && reset($this->_get_args) === NULL) + if (isset($this->_supported_formats[$format]) === TRUE) { - $pattern = '/\.(' . implode('|', array_keys($this->_supported_formats)) . ')$/'; - $matches = []; - - if (preg_match($pattern, key($this->_get_args), $matches)) - { - return $matches[1]; - } + return $format; } } From e545523dcb202ba117c4c69c095220a0420ef4e6 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:06:48 +0300 Subject: [PATCH 05/17] Updating the example API. --- application/controllers/api/Example.php | 67 ++++++++++--------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index c221ea9c..72da4cc2 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -30,25 +30,18 @@ function __construct() $this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key } - public function users_get($id_param = NULL) + public function users_get() { // Users from a data store e.g. database // $user = $this->some_model->getSomething($id); $users = [ - 1 => ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], - 2 => ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], - 3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], + ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], + ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], + ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], ]; - // Get the id parameter value $id = $this->get('id'); - // If NULL, then check the id passed as users/:id - if ($id === NULL) - { - $id = $id_param; - } - // If the id parameter and query parameter don't exist, return all users instead if ($id === NULL) { @@ -66,28 +59,34 @@ public function users_get($id_param = NULL) 'error' => 'No users were found' ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code } - } - // Check if the id is a valid integer - if (ctype_digit($id)) - { - // Cast as an int - $id = (int) $id; - } + $id = (int) $id; - // If not a valid id + // Validate the id. if ($id <= 0) { - // Set the response and exit + // 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, by retrieving the id from the GET request - $user = isset($users[$id]) ? $users[$id] : NULL; + // 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 a user exists in the data store e.g. database - if ($user) + if (!empty($user)) { $this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code } @@ -113,25 +112,11 @@ public function users_post() $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code } - public function users_delete($id_param = NULL) + public function users_delete() { - // Get the id parameter value - $id = $this->get('id'); - - // If NULL, then check the id passed as users/:id - if ($id === NULL) - { - $id = $id_param; - } - - // Check if the id is a valid integer - if (ctype_digit($id)) - { - // Cast as an int - $id = (int) $id; - } + $id = (int) $this->get('id'); - // If not a valid id + // Validate the id. if ($id <= 0) { // Set the response and exit From 61938ec5c2028338acf871ac8195307c71d31b5c Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:08:36 +0300 Subject: [PATCH 06/17] Key.php: Tunning whitespace. --- application/controllers/api/Key.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/application/controllers/api/Key.php b/application/controllers/api/Key.php index 0e97016c..bfc7f6fe 100644 --- a/application/controllers/api/Key.php +++ b/application/controllers/api/Key.php @@ -218,7 +218,7 @@ private function _generate_key() // If an error occurred, then fall back to the previous method if ($salt === FALSE) { - $salt = hash('sha256', time() . mt_rand()); + $salt = hash('sha256', time() . mt_rand()); } $new_key = substr($salt, 0, config_item('rest_key_length')); } @@ -233,16 +233,16 @@ private function _generate_key() private function _get_key($key) { return $this->db - ->where(config_item('rest_key_column'), $key) - ->get(config_item('rest_keys_table')) - ->row(); + ->where(config_item('rest_key_column'), $key) + ->get(config_item('rest_keys_table')) + ->row(); } private function _key_exists($key) { return $this->db - ->where(config_item('rest_key_column'), $key) - ->count_all_results(config_item('rest_keys_table')) > 0; + ->where(config_item('rest_key_column'), $key) + ->count_all_results(config_item('rest_keys_table')) > 0; } private function _insert_key($key, $data) From 7be0147172b96ee3fdc5bd0b8fc66e625d65a2f0 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:12:11 +0300 Subject: [PATCH 07/17] Key.php: Some comment corrections. --- application/controllers/api/Key.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/controllers/api/Key.php b/application/controllers/api/Key.php index bfc7f6fe..a986569f 100644 --- a/application/controllers/api/Key.php +++ b/application/controllers/api/Key.php @@ -126,7 +126,7 @@ public function level_post() } /** - * Change the level + * Suspent a key * * @access public * @return void @@ -163,7 +163,7 @@ public function suspend_post() } /** - * Remove a key from the database to stop it working + * Regenerate a key * * @access public * @return void From dc397bfac22fe78b71f74de4b9d240bab4c889e2 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:17:31 +0300 Subject: [PATCH 08/17] Adding some index.html files for protection. --- application/config/index.html | 11 +++++++++++ application/controllers/api/index.html | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 application/config/index.html create mode 100644 application/controllers/api/index.html diff --git a/application/config/index.html b/application/config/index.html new file mode 100644 index 00000000..b702fbc3 --- /dev/null +++ b/application/config/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + diff --git a/application/controllers/api/index.html b/application/controllers/api/index.html new file mode 100644 index 00000000..b702fbc3 --- /dev/null +++ b/application/controllers/api/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + From cee599d24ebb2a32ae3fc0cf9830ecc9d330ab72 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:21:22 +0300 Subject: [PATCH 09/17] Adding routes.php configuration file. --- application/config/routes.php | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 application/config/routes.php diff --git a/application/config/routes.php b/application/config/routes.php new file mode 100644 index 00000000..a56d21f6 --- /dev/null +++ b/application/config/routes.php @@ -0,0 +1,62 @@ + my_controller/index +| my-controller/my-method -> my_controller/my_method +*/ +$route['default_controller'] = 'welcome'; +$route['404_override'] = ''; +$route['translate_uri_dashes'] = TRUE; + +/* +| ------------------------------------------------------------------------- +| Sample REST API Routes +| ------------------------------------------------------------------------- +*/ +$route['api/example/users/(:num)'] = 'api/example/users/id/$1'; // Example 4 +$route['api/example/users/(:num)(\.)([a-zA-Z0-9_-]+)(.*)'] = 'api/example/users/id/$1/format/$3$4'; // Example 8 From 1da1b9fbe183d5ced9d22c7c6cbc7b431adf19c7 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:24:49 +0300 Subject: [PATCH 10/17] Adding a modified "Welcome" page and a homepage for quick tests. --- application/controllers/Rest_server.php | 12 ++ application/controllers/Welcome.php | 27 +++++ application/controllers/index.html | 11 ++ application/views/index.html | 11 ++ application/views/rest_server.php | 155 ++++++++++++++++++++++++ application/views/welcome_message.php | 92 ++++++++++++++ 6 files changed, 308 insertions(+) create mode 100644 application/controllers/Rest_server.php create mode 100644 application/controllers/Welcome.php create mode 100644 application/controllers/index.html create mode 100644 application/views/index.html create mode 100644 application/views/rest_server.php create mode 100644 application/views/welcome_message.php diff --git a/application/controllers/Rest_server.php b/application/controllers/Rest_server.php new file mode 100644 index 00000000..7ceef858 --- /dev/null +++ b/application/controllers/Rest_server.php @@ -0,0 +1,12 @@ +load->helper('url'); + + $this->load->view('rest_server'); + } +} diff --git a/application/controllers/Welcome.php b/application/controllers/Welcome.php new file mode 100644 index 00000000..aa64c8d4 --- /dev/null +++ b/application/controllers/Welcome.php @@ -0,0 +1,27 @@ + + * @see http://codeigniter.com/user_guide/general/urls.html + */ + public function index() + { + $this->load->helper('url'); + + $this->load->view('welcome_message'); + } +} diff --git a/application/controllers/index.html b/application/controllers/index.html new file mode 100644 index 00000000..b702fbc3 --- /dev/null +++ b/application/controllers/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + diff --git a/application/views/index.html b/application/views/index.html new file mode 100644 index 00000000..b702fbc3 --- /dev/null +++ b/application/views/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + diff --git a/application/views/rest_server.php b/application/views/rest_server.php new file mode 100644 index 00000000..8387dbda --- /dev/null +++ b/application/views/rest_server.php @@ -0,0 +1,155 @@ + + + + + REST Server Tests + + + + + +
+

REST Server Tests

+ +
+ +

Home

+ +

+ See the article + + http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/ + +

+ +

+ The master project repository is + + https://github.com/chriskacerguis/codeigniter-restserver + +

+ +

+ Click on the links to check whether the REST server is working. +

+ +
    +
  1. Users - defaulting to JSON
  2. +
  3. Users - get it in CSV
  4. +
  5. User #1 - defaulting to JSON (users/id/1)
  6. +
  7. User #1 - defaulting to JSON (users/1)
  8. +
  9. User #1 - get it in XML (users/id/1.xml)
  10. +
  11. User #1 - get it in XML (users/id/1/format/xml)
  12. +
  13. User #1 - get it in XML (users/id/1?format=xml)
  14. +
  15. User #1 - get it in XML (users/1.xml)
  16. +
  17. Users - get it in JSON (AJAX request)
  18. +
  19. Users - get it in HTML (users.html)
  20. +
  21. Users - get it in HTML (users/format/html)
  22. +
  23. Users - get it in HTML (users?format=html)
  24. +
+ +
+ + +
+ + + + + + + \ No newline at end of file diff --git a/application/views/welcome_message.php b/application/views/welcome_message.php new file mode 100644 index 00000000..33c29458 --- /dev/null +++ b/application/views/welcome_message.php @@ -0,0 +1,92 @@ + + + + + Welcome to CodeIgniter + + + + + +
+

Welcome to CodeIgniter!

+ +
+ +

REST Server Tests

+ +

The page you are looking at is being generated dynamically by CodeIgniter.

+ +

If you would like to edit this page you'll find it located at:

+ application/views/welcome_message.php + +

The corresponding controller for this page is found at:

+ application/controllers/Welcome.php + +

If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.

+
+ + +
+ + + \ No newline at end of file From b44d815113fbeb5e2dc8234dd8259e7a914710c8 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 17:45:41 +0300 Subject: [PATCH 11/17] Key.php: Replacing (self::) usages with ($this->) The context is not static. --- application/controllers/api/Key.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/application/controllers/api/Key.php b/application/controllers/api/Key.php index a986569f..0f9cb07c 100644 --- a/application/controllers/api/Key.php +++ b/application/controllers/api/Key.php @@ -34,14 +34,14 @@ class Key extends REST_Controller { public function index_put() { // Build a new key - $key = self::_generate_key(); + $key = $this->_generate_key(); // If no key level provided, provide a generic key $level = $this->put('level') ? $this->put('level') : 1; $ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1; // Insert the new key - if (self::_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits])) + if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits])) { $this->response([ 'status' => TRUE, @@ -68,7 +68,7 @@ public function index_delete() $key = $this->delete('key'); // Does this key exist? - if (!self::_key_exists($key)) + if (!$this->_key_exists($key)) { // It doesn't appear the key exists $this->response([ @@ -78,7 +78,7 @@ public function index_delete() } // Destroy it - self::_delete_key($key); + $this->_delete_key($key); // Respond that the key was destroyed $this->response([ @@ -99,7 +99,7 @@ public function level_post() $new_level = $this->post('level'); // Does this key exist? - if (!self::_key_exists($key)) + if (!$this->_key_exists($key)) { // It doesn't appear the key exists $this->response([ @@ -109,7 +109,7 @@ public function level_post() } // Update the key level - if (self::_update_key($key, ['level' => $new_level])) + if ($this->_update_key($key, ['level' => $new_level])) { $this->response([ 'status' => TRUE, @@ -136,7 +136,7 @@ public function suspend_post() $key = $this->post('key'); // Does this key exist? - if (!self::_key_exists($key)) + if (!$this->_key_exists($key)) { // It doesn't appear the key exists $this->response([ @@ -146,7 +146,7 @@ public function suspend_post() } // Update the key level - if (self::_update_key($key, ['level' => 0])) + if ($this->_update_key($key, ['level' => 0])) { $this->response([ 'status' => TRUE, @@ -171,7 +171,7 @@ public function suspend_post() public function regenerate_post() { $old_key = $this->post('key'); - $key_details = self::_get_key($old_key); + $key_details = $this->_get_key($old_key); // Does this key exist? if (!$key_details) @@ -184,13 +184,13 @@ public function regenerate_post() } // Build a new key - $new_key = self::_generate_key(); + $new_key = $this->_generate_key(); // Insert the new key - if (self::_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits])) + if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits])) { // Suspend old key - self::_update_key($old_key, ['level' => 0]); + $this->_update_key($old_key, ['level' => 0]); $this->response([ 'status' => TRUE, @@ -220,10 +220,10 @@ private function _generate_key() { $salt = hash('sha256', time() . mt_rand()); } + $new_key = substr($salt, 0, config_item('rest_key_length')); } - while (self::_key_exists($new_key)); - // Already in the DB? Fail. Try again + while ($this->_key_exists($new_key)); return $new_key; } From 9d87d1355da6eef7c546506c429035153b815ade Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 18:26:52 +0300 Subject: [PATCH 12/17] Fixing a typo. --- application/controllers/api/Key.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/controllers/api/Key.php b/application/controllers/api/Key.php index 0f9cb07c..e2510764 100644 --- a/application/controllers/api/Key.php +++ b/application/controllers/api/Key.php @@ -126,7 +126,7 @@ public function level_post() } /** - * Suspent a key + * Suspend a key * * @access public * @return void From 1c8cd8fa93d93d3cfe97a7042e960e24f6973bbf Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 18:57:06 +0300 Subject: [PATCH 13/17] Contemporary code for an AJAX call. --- application/views/rest_server.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/application/views/rest_server.php b/application/views/rest_server.php index 8387dbda..aef7702e 100644 --- a/application/views/rest_server.php +++ b/application/views/rest_server.php @@ -127,23 +127,22 @@ url: $(this).attr("href"), // URL from the link that was clicked on. - success: function(data, textStatus, jqXHR) { + }).done(function (data) { - // The 'data' parameter is an array of objects that can be looped over. + // The 'data' parameter is an array of objects that can be looped over. - if (window.JSON && window.JSON.stringify) { + if (window.JSON && window.JSON.stringify) { - // Let us display data in this example - // in a relatively friendly manner. - data = window.JSON.stringify(data); - } + // Let us display data in this example + // in a relatively friendly manner. + data = window.JSON.stringify(data); + } - alert(data); - }, + alert(data); - error: function(jqXHR, textStatus, errorThrown) { - alert('Oh no! A problem with the AJAX request!'); - } + }).fail(function () { + + alert('Oh no! A problem with the AJAX request!'); }); }); }); From de4d8d17884998b302e3f95c4b3fd87c55b33c33 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 19:05:05 +0300 Subject: [PATCH 14/17] window.JSON.stringify() exist in all normal browsers, removing the ckeck. --- application/views/rest_server.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/application/views/rest_server.php b/application/views/rest_server.php index aef7702e..ffcdd7d8 100644 --- a/application/views/rest_server.php +++ b/application/views/rest_server.php @@ -131,14 +131,7 @@ // The 'data' parameter is an array of objects that can be looped over. - if (window.JSON && window.JSON.stringify) { - - // Let us display data in this example - // in a relatively friendly manner. - data = window.JSON.stringify(data); - } - - alert(data); + alert(window.JSON.stringify(data)); }).fail(function () { From 913d0741545288c4ad373bc48ae063d4d83851d7 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 20:52:04 +0300 Subject: [PATCH 15/17] config/rest.php: Whitespace tunning. --- application/config/rest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/config/rest.php b/application/config/rest.php index d75c8696..2e914fe6 100644 --- a/application/config/rest.php +++ b/application/config/rest.php @@ -160,9 +160,9 @@ | | example: | -| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none'; -| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none'; -| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none'; +| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none'; +| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none'; +| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none'; */ // ---Uncomment list line for the wildard unit test From 83f0c1c820bd6e25ac8bbaec33162dcf2b9aa32c Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 21:16:03 +0300 Subject: [PATCH 16/17] REST_Controller.php: Whitespace tuning. --- application/libraries/REST_Controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/libraries/REST_Controller.php b/application/libraries/REST_Controller.php index bf8d9362..5ebe05e9 100644 --- a/application/libraries/REST_Controller.php +++ b/application/libraries/REST_Controller.php @@ -657,10 +657,10 @@ public function _remap($object_called, $arguments) { // If the method doesn't exist, then the error will be caught and an error response shown $this->response([ - $this->config->item('rest_status_field_name') => FALSE, - $this->config->item('rest_message_field_name') => [ - 'classname' => get_class($ex), - 'message' => $ex->getMessage() + $this->config->item('rest_status_field_name') => FALSE, + $this->config->item('rest_message_field_name') => [ + 'classname' => get_class($ex), + 'message' => $ex->getMessage() ] ], self::HTTP_INTERNAL_SERVER_ERROR); } From c82d05ef502fb1fcc130a83e8d553f6fb1b9f95f Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 23 Jul 2015 21:35:02 +0300 Subject: [PATCH 17/17] Example.php: Some comment modifications. --- application/controllers/api/Example.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index 72da4cc2..479d790c 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -33,7 +33,6 @@ function __construct() public function users_get() { // Users from a data store e.g. database - // $user = $this->some_model->getSomething($id); $users = [ ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], @@ -42,7 +41,8 @@ public function users_get() $id = $this->get('id'); - // If the id parameter and query parameter don't exist, return all users instead + // 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) @@ -61,6 +61,8 @@ public function users_get() } } + // Find and return a single record for a particular user. + $id = (int) $id; // Validate the id.