1

To create simple Rest API I have followed below steps downloaded CodeIgniter-restserver and copy pasted REST_Controller from downloaded file into libraries under my project(src is the project name). And then created Api.php inside controller of my project

<?php
require(APPPATH'.libraries/REST_Controller.php');

class API extends REST_Controller {

    function test()
    {
        echo "RESTfull API";
    }
}
?>

And I run the URLhttp://localhost/src/index.php/Api/test in postman but it is not showing results.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
GonnaHack
  • 57
  • 1
  • 3
  • 12
  • I suggest you enable error reporting in your `php.ini` file, ie `error_reporting = E_ALL` and `display_errors = On` – Phil Jul 11 '18 at 06:43

5 Answers5

3

You need to follow the below link https://itsolutionstuff.com/post/codeigniter-3-restful-api-tutorialexample.html

and then after you run the code you will get a small error Unable to load the requested language file: language/english/rest_controller_lang.php

The problem is that codeigniter can't find the rest_controller translations. You just need to create this file /application/languages/english/rest_controller_lang.php

Then copy & paste this code inside:

<?php
/*
 * English language
 */
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';

Hope this helps

Jagadish Meghval
  • 199
  • 1
  • 11
1

Please read this article line by line. This is the best solution for beginners to use CodeIgniter Rest API library.

<?php
require(APPPATH.'/libraries/REST_Controller.php');

class API extends REST_Controller {

    function test_get()
    {

    $data = array("message"=>"RESTfull API");
    $this->response($data);
    }
}
?>

call: http://localhost/src/index.php/Api/test

Note: In rest API you should define the method type as GET, POST, PUT, and DELETE

How to use the Rest API library in CodeIgniter

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
1

Hope this will help you :

require APPPATH . '/libraries/REST_Controller.php';
class Api extends REST_Controller {

    function test_get()
    {
      $data = array('response' => 'RESTfull API');

      if(count($data ) > 0)
      {
         $this->response($data ,REST_Controller::HTTP_OK);
      }
      else
      {
         $error = array('message' => 'No record found');
         $this->response($error,REST_Controller::HTTP_OK);
      }   
}  

For more pls read : https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Your class name should be `Api` change it from `API` and hit url like this `http://localhost/src/index.php/Api/test` or use like this `http://localhost/src/index.php/api/test` – Pradeep Jul 11 '18 at 07:11
  • some thing is wrong on your side : see this : https://imgur.com/a/TlEna3u – Pradeep Jul 11 '18 at 08:08
  • can you tell me what steps you followed.. I followed above steps it is not working so i'm asking again.. could you please tell me – GonnaHack Jul 11 '18 at 08:12
0

try

<?php
    require(APPPATH'.libraries/REST_Controller.php');

    class ApiController extends REST_Controller {

        function test()
        {
            echo "RESTfull API";
            die;
        }
    }
?>

I suggest to use built-in method response from the library

vnt
  • 611
  • 6
  • 13
  • @GonnaHack: consider to rename your class name to `ApiController` and the filename as well, should be `ApiController.php` – vnt Jul 11 '18 at 07:18
0

Try lowercase: api/test instead of Api/test

Upd. Add to config/routes.php

$route['api/test'] = 'api/test';

Another one thing, if you have routes try figure out, does they use pluralize, if it foes, so you can try to check /apis/test instead of /api/test because pluralize transform controller name from one to many form (sorry for primitive English)

Yelnar
  • 81
  • 1
  • 4