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

  • 普通网友 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条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大