3

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.

1
  • I suggest you enable error reporting in your php.ini file, ie error_reporting = E_ALL and display_errors = On Commented Jul 11, 2018 at 6:43

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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
some thing is wrong on your side : see this : imgur.com/a/TlEna3u
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
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

8 Comments

I tried same nothing showing in postman and followed same doc only
What issue is you getting now?
Yeah I opened nothing it is showing in browser also :(
you mean, it's diaplying blank page. if it is then enable error reporting as stackoverflow.com/questions/9587413/…
Change controller name API to Api in Api.php
|
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

1 Comment

@GonnaHack: consider to rename your class name to ApiController and the filename as well, should be ApiController.php
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)

3 Comments

Did you have a router? And i should ask, you run it under apache/nginx at 80 port?
Do you add route to config/routes.php file?
I changed but nothing is working.. even I changed in routes also

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.