doushi1912
2017-07-18 11:07
浏览 63

如何在Codeigniter中创建API?

I want to create an API in Codeigniter which will get the cart_list and cart_list will have latitude, longitude of a store, I want to check the distance between post parameters(latitude, longitude) and return the distance.

Now, I am trying to create a controller in controllers folder of Codeigniter and written a function to get the cart_list but I am unable to get the list, I just tried to return 'success' as in response to this api but I am getting response as

{
    "status": false,
    "error": "Unknown method."
} 

distance_calculator

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH.'/libraries/api/REST_Controller.php';


class distance_calculator extends REST_Controller
{
    function distance_calculator()
    {
        parent::__construct();
        $this->load->model('mdl_cart_web');
    }


    function cart_list($offset = 0)
    {
        $limit = '';

        $data = array();

        $s_data = $_POST;

        $carts = $this->mdl_cart_web->get_cart_list($limit,$offset,$s_data)->result_array();
        $totalRows = $this->mdl_cart_web->get_total_cart_product($s_data)->num_rows();

      //  $data = $this->mdl_common->pagination_data('cart/get_cart_list/',$totalRows,$limit,'show_data');
        $data['carts'] = $carts;
        $data['total_cart'] = $totalRows;

        $this->response('success', 200);
    }
}

?>

I also declared route in routes file

routes

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['default_controller'] = "home1";

$route['cart_list'] = 'distance_controller/cart_list';

I am calling this function as

http://test.pricewhirl.com/index.php/distance_calculator/cart_list

with GET method,and in response I get error unknown method.

I am a beginner in web development. Please help with this, thank you.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doumian0569 2017-07-18 12:03
    已采纳

    always add method name as post fix in code. like if you wrote a function with post method use -> cart_list_post() and if you wrote a function with get method use -> cart_list_get()

    点赞 评论
  • doutao1282 2017-07-18 11:10

    You must define the type of request in the method name :

    function cart_list_get() {
        ....
    }
    

    From doc :

    When your controller extends from REST_Controller, 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.

    点赞 评论
  • dps69208 2017-07-18 11:52

    If you are using Rest Controller you have to use Postfix _post for POST method and _get for GET method in function like

    public function abc_post()
    
    点赞 评论
  • duanke0178 2017-07-19 11:44

    I know this isn't an answer, but if you need documentation check out this article on how to make an api out of CI

    https://www.cloudways.com/blog/rest-api-in-codeigniter/

    点赞 评论

相关推荐 更多相似问题