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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵