Codeigniter chriskacerguis-RESTAPI未知的访问方法

时间:2016-09-13 07:17:52

标签: php codeigniter api

这是我的文件夹

Controller
  L>API
    L>ID
      L>Example.php
  L>Example.php

我在API / example.php上的example.php和API / ID / example.php都是这样的。

<?php

defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/core/REST_Controller.php';

/**
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array
 *
 * @package         CodeIgniter
 * @subpackage      Rest Server
 * @category        Controller
 * @author          Phil Sturgeon, Chris Kacerguis
 * @license         MIT
 * @link            https://github.com/chriskacerguis/codeigniter-restserver
 */

class Example extends REST_Controller {
    public function index_get()
    {
        // index from a data store e.g. database
        $index = [
            ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
            ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
            ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
        ];

        $id = $this->get('id');
        $this->response($index, REST_Controller::HTTP_OK);
    }

    public function users_get(){
       echo"test";die();
    }
}

首先我像这样调整我的config / routes.php

$route['API'] = "API";
$route['API/(.+)'] = "API/$1";

当我访问base_url / API / Example / id / 1时,它响应了名为John的$ index [1]。

然后我将routes.php改为

$route["API/example"]="API/ID/example";
$route["API/example/(.+)"]="API/ID/example/(.+)";

然后我访问了我访问过的base_url / API / Example / id / 1 这将基于routes.php转到API / ID / Example 但它回应了

{
"status": false
"message": "Unknown method"
}

但是如果我加入了base_url / API / Example?id = 1 它以$ index [1]

回应

如何使用base_url / API / example / id / 1从API / ID / Example.php调用api 它将返回$ index [1]?不是状态:错误的未知方法

1 个答案:

答案 0 :(得分:0)

我知道解决方案, 这是因为我的路由错误

$route['API/search_location/(.+)']="API/ID/example/index/$1";
$route['API/search_location']="API/ID/example";