doushi1912
2017-07-18 11:07
浏览 64
已采纳

如何在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条回答 默认 最新

相关推荐 更多相似问题