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()

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(3条)

相关推荐 更多相似问题