m4dem4n/rest-framework

PHP RESTful API 框架

dev-master 2019-02-06 16:44 UTC

This package is auto-updated.

Last update: 2024-09-12 13:47:35 UTC


README

这是我用PHP自己编写的一个简单的REST框架。

开始使用

以下列出的"文件"展示了此框架运行所需的最小文件。我可能会在未来提供更复杂的示例来演示如认证等高级功能。

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?request=$1 [QSA,NC,L]
</IfModule>

index.php

<?php
define('REST_FRAMEWORK', '/path/to/RESTFramework/');
require_once(REST_FRAMEWORK . 'bootstrap.php');
include_once('myapi.class.php');

/**
 * The request parameter is populated by a rule in .htaccess. The .htaccess
 * rule takes the request URI, sans the domain name, and passes it as a
 * parameter named 'request' to this page.
 */
$request = (!empty($_REQUEST['request']) ? $_REQUEST['request'] : '');

/**
 * Run your API
 */
$MyAPI = new MyAPI($request);
$MyAPI->run();

myapi.class.php

<?php
class MyAPI extends RESTFramework {

    public function __construct($request)
    {
        // Parent constuctor must be called first.
        parent::__construct($request);

        // Create an endpoint. The string parameter must match a function name.
        $this->addEndpoint('hello');
    }

    // Endpoint function
    public function hello()
    {
        $this->contentType = self::CONTENT_TYPE_PLAIN; // json is the default.
        return 'Hello world!';
    }
}