hiraq/drapi

创建和管理API服务的框架

v1.0.0-beta1 2013-07-27 16:56 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:05:13 UTC


README

创建和管理API (REST)服务的框架

Build Status Coverage Status Latest Stable Version Total Downloads project status

用法

将以下代码放入您的 index.php 文件中。

<?php
require_once 'vendor/autoload.php';

use Drapi\Router;
use Drapi\Request as DrapiRequest;
use Drapi\Response as DrapiResponse;
use Drapi\Handler;
use Drapi\Compiler;

try {

	$router = new Router;
	$request = new DrapiRequest;
	$response = new DrapiResponse;
	$handler = new Handler;

	$compiler = new Compiler($router,$request,$response);
	$compiler->setHandlerNameSpace('Drapi\\Handler\\');
	$compiler->setHandler($handler);
	$compiler->compile();

} catch (Exception $e) {
	echo $e->getMessage();
}

然后在浏览器中打开,访问

  1. https:///myapi/testhttps:///myapi/test/get
  2. https:///myapi/testing 用于失败的响应示例
  3. http://php.drapi/test/test_param?key1=val1&key2=val2 用于GET参数示例

Drapi不需要任何重写规则!

处理器

处理器是一个类,用于管理并从您的数据源(MySQL、Postgre、Mongodb等)返回数据。
在Drapi/Handler内部创建一个处理器类,例如 Users.php。每当服务器收到基于:'/users/get' 的API请求时,您的处理器(Users.php)将被加载,并将使用Users类中的 get 方法。参见Drapi/Handler中的Test.php示例。

插件

插件是处理器类的扩展,在Drapi/Handler/Plugin中创建一个类,例如Mongo.php,并在处理器类中加载此类(自动加载)。

响应

所有Drapi响应都是格式化的json数据。如果请求失败调用处理器或没有有效数据可用,则Drapi将自动返回错误状态和消息。

许可证

Drapi根据 BSD 3-Clause 许可证 发布。有关详细信息,请参阅LICENSE.txt文件。