geekcow/fony-core

Fony PHP API 的核心类

0.0.50 2023-05-03 15:33 UTC

README

Fony PHP API 框架的核心类

用法

您可以通过声明一个基础 API 类(如下例所示)将 fony 核心添加为 API 的依赖项:

<?php
require 'vendor/autoload.php';
define('MY_DOC_ROOT', __DIR__);
define('MY_ASSET_ROOT', __DIR__);

use Geekcow\FonyCore\FonyApi;
use {PROJECTNAMESPACE}\router;

// Requests from the same server don't have a HTTP_ORIGIN header
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
  $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}

try {
  $router = new Router('{PROJECTCONFIGFILE}');
  $API = new FonyApi($_REQUEST['request'], $router);
  // if you want to have the origin set:
  // $API = new FonyApi($_REQUEST['request'], $router, $_SERVER['HTTP_ORIGIN']);
  echo $API->processAPI();
} catch (\Exception $e) {
  echo json_encode(Array('error' => $e->getMessage()));
}

?>

您的 Router 类应该是此处提供的 FonyApi 类的实现。

Router 的工作方式

Router 类的工作方式如下

  • 构造函数会遍历请求的端点。因此,一个动作类(也称为控制器)可以预先放置。
  • 端点将执行一个与请求同名的函数。例如,如果端点是 /api/user/:id,则我们将要执行的端点是 user,因此需要定义一个 user() 函数。
  • 此外,如果端点是用破折号编写的,则端点应该用驼峰式编写。

这是一个 Router 类的示例

<?php

namespace {PROJECTNAMESPACE};

use Geekcow\FonyCore\Controller\GenericController;
use Geekcow\FonyCore\FonyRouter;
use Geekcow\FonyCore\Utils\SessionUtils;
use {PROJECTNAMESPACE}\model\TestModel;

class Router extends FonyRouter
{
    public function __construct($config_file)
    {
        parent::__construct($config_file);
    }
    
    public function prestageEndpoints($endpoint, $request){
        parent::prestageEndpoints($endpoint, $request);

        switch ($this->endpoint) {
            case 'generic-controller-endpoint':
                $this->action = new GenericController();
                $this->action->setRequest($request);
                $this->action->setModel(new TestModel());
                $this->action->setFilter(['fields', 'that', 'you', 'do not', 'want', 'to', 'show']);
                $this->setAllowedRoles(Allow::CUSTOMROLE());
                break;
            case 'generic-actionable-controller-endpoint':
                $this->action = new GenericActionController();
                $this->action->setRequest($request);
                $this->action->setModel(new TestModel());
                $this->action->setFilter(['fields', 'that', 'you', 'do not', 'want', 'to', 'show']);
                $this->setAllowedRoles(Allow::CUSTOMROLE());
                break;
        }
    }
    
    /**
     * Shows a welcome message
     *
     * @return string
     *
     */
    //WELCOME MESSAGE
    public function welcome()
    {
        if ($this->method == 'GET') {
            return "WELCOME TO FONY PHP";
        } else {
            return "Invalid Method";
        }
    }

    /**
     * Executes only a POST operation.
     *
     * @return JSON Authenticated response with token
     *
     */
    public function genericControllerEndpoint()
    {
        switch ($this->method) {
            case 'POST':
                $this->action->doPost($this->args, $this->verb);
                $this->response_code = $this->action->response['code'];
                return $this->action->response;
                break;
            case 'OPTIONS':
                exit(0);
                break;
            default:
                $this->response_code = 405;
                return "Invalid method";
                break;
        }
    }

    /**
     * Executes an endpoint using the default behavior.
     *
     * @return JSON response
     *
     */
    public function genericActionableControllerEndpoint()
    {
        return $this->executesCall();
    }
}

会话处理

默认情况下,Fony 使用 OAuth2 身份验证,因此它依赖于安装 Fony 所需的配置文件

  • 参数列表待定

或者您可以定义自己的身份验证机制(例如 fony-auth 项目),在那里您可以创建一个实现 AuthenticatorInterface 的身份验证类,并在 Router 构造函数中初始化它

    $sessionInstance = SessionUtils::getInstance(new CustomAuthenticatorClass());

示例

很快将有一个示例存储库可用