0

I created a controller named Api.php then I extended the Rest_Controller. I noticed that I can only use index_get() when creating a function in this controller

<?php

class Api extends REST_Controller{

    public function __construct()
    {
        parent::__construct();

    }

    public function index_get(){
        $car_id = $this->get('car_id');
        if(!$car_id){

            $this->response("No Car ID specified", 400);

            exit;
        }

        $result = $this->model_getvalues->getCars( $car_id );

        if($result){

            $this->response($result, 200); 

            exit;
        } 
        else{

             $this->response("Invalid Car ID", 404);

            exit;
        }
    }

}

but when I try creating my desired function like getAllCars() instead of index_get() I get an error message telling me unknown function.

How can I define my own function instead of using index_get() when using rest api library in CodeIgniter?

11
  • take a look here github.com/chriskacerguis/…, i think you missed this part: the method names will be appended with the HTTP method used to access the request. If you're making an HTTP GET call to /books, for instance, it would call a Books#index_get() method Commented Sep 19, 2018 at 22:21
  • 1
    I get what you're saying, the thing is can the index_ be dynamic such that I can name a method getCars_get(), getById_get() and so on and so forth Commented Sep 19, 2018 at 22:37
  • yes, thats exactly what i meant, but to be honest getCars_get is a bit redundant don't you think ? since you have the _get suffix already in there just name your method cars_get() and if you've a post request you can call your method cars_post() - but from outside its always the same namely /cars/ Commented Sep 20, 2018 at 5:37
  • the thing is i'm new to using rest api in codeigniter, if i use cars_get() of which it's working, what if i want to fetch based on some conditions, how will i go about that @sintakonte Commented Sep 20, 2018 at 6:16
  • what do you mean with conditions ? something like /cars/?id=123 ? Commented Sep 20, 2018 at 6:50

3 Answers 3

1

Thanks i have been able to figure it out, i just found out that the name before the _get is what matters to the url i.e when one has a method like getCars_get, you will have to call it using just getCars without the _get attach to it, it work for me. it means that one can have more than _get method in the API controller.

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

Comments

0

By default on https://github.com/chriskacerguis/codeigniter-restserver#handling-requests the method is index_get(),another way to use your own method is play with the HTTP GET parameter ex:

if($this->get('car_id') == 'all'){
  //your own function here
}

Or if you really want to create your own method you can refer on this http://programmerblog.net/create-restful-web-services-in-codeigniter/

8 Comments

define it in your route file.try like this route['index_get']='api/index_get';
i don't get you @ferry sanjaya
did you read that article? if you want custom method like getAllCars() instead of index_get(),you must put suffix '_get' so getAllCars_get(),thats my opinion
i have tried that, but it's still throwing an error say unknown method
i tried it and works,do you test using postman and using GET request??.. show me your whole code which return error
|
0

All you have to do is change function index_get() to getAllCars_get(),

`<?php

class Api extends REST_Controller{

public function __construct()
{
    parent::__construct();

}

public function getAllCars_get(){
    
   //your code
    
}

} ?> ` Like this

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.