【发布时间】:2019-11-27 01:30:30
【问题描述】:
我正在尝试为现有数据库构建一个 rest API。有人建议我使用 Chris Kacerguis 的 Rest API,但他的最新版本经常出现一些错误。然后我决定使用仍然有效但出现“不支持的协议”错误的先前版本。我从他的基本 GET 示例中复制粘贴了代码,但仍然有相同的不受支持的协议错误。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use chriskacerguis\RestServer\RestController;
class Api extends RestController {
function __construct()
{
// Construct the parent class
parent::__construct();
}
public function users_get()
{
// Users from a data store e.g. database
$users = [
['id' => 0, 'name' => 'John', 'email' => 'john@example.com'],
['id' => 1, 'name' => 'Jim', 'email' => 'jim@example.com'],
];
$id = $this->get( 'id' );
if ( $id === null )
{
// Check if the users data store contains users
if ( $users )
{
// Set the response and exit
$this->response( $users, 200 );
}
else
{
// Set the response and exit
$this->response( [
'status' => false,
'message' => 'No users were found'
], 404 );
}
}
else
{
if ( array_key_exists( $id, $users ) )
{
$this->response( $users[$id], 200 );
}
else
{
$this->response( [
'status' => false,
'message' => 'No such user found'
], 404 );
}
}
}
}
【问题讨论】:
标签: php rest codeigniter