maikwoehl/rest-recipes

REST api 的类和脚本集合

0.1.6 2016-05-31 19:04 UTC

This package is auto-updated.

Last update: 2024-09-08 22:17:29 UTC


README

路由器

将请求路由到特定的端点并提取变量。

路由必须以 '/' 开头。

路由的参数顺序和路由函数的参数顺序必须相同。

路由的顺序必须像这样

  • /recipe/
  • /recipe/create (例如,默认数据和ID作为响应)
  • /recipe/create/<name> (特定名称用于创建和ID作为响应)
  • /recipe/<id>
  • /recipe/<id>/export
  • /recipe/<id>/<someVar>

用法

<?php
/**
 * api.php
 *
 * @version 0.1
 *
 */

require_once "RestRecipes/autoload.php";

use RestRecipes\Router;

$app = new Router();

// Index
$app->route("/recipe/", "GET", function() {
    
});

// Detail
$app->route("/recipe/<id>", "GET", function($id) {
    
});

// Modify Object
$app->route("/recipe/<id>", "PUT", function($id, $data) {
    
});


try {
    $app->run();
} catch (RuntimeException $e) {
    // Show some information that no API endpoint was called
}

Router类的默认querySelector是$q。您可以像这样调用API

GET api.php?q=/recipe/
GET api.php?q=/recipe/3

认证提供者

认证提供者允许检查某些安全选项。

用法

<?php
/**
 * api.php
 *
 * @version 0.1
 *
 */

require_once "RestRecipes/autoload.php";

use RestRecipes\Router;
use RestRecipes\AuthenticationProvider;

$security = new AuthenticationProvider(AuthenticationProvider::AUTH_HTTP_BASIC);
$app = new Router();


// Secure API route
$app->route("/secure/", "GET", function() {
    $security->setHttpBasicAuthenticationCredentials("user", "password");
    
    if (!$security->authenticate())
        return false;
});

// Index
$app->route("/recipe/", "GET", function() {
    
});

// Detail
$app->route("/recipe/<id>", "GET", function($id) {
    
});

try {
    $app->run();
} catch (RuntimeException $e) {
    // Show some information that no API endpoint was called
}