skyline/api

Skyline API 包提供了一个动作控制器来管理 API 请求

v8.0.3 2023-03-05 09:23 UTC

This package is auto-updated.

Last update: 2024-09-05 12:29:21 UTC


README

Skyline API 包为您提供了动作控制器来管理 API 请求。
它还包括 Skyline API 组件。

安装

$ composer require skyline/api

您可能还对 skyline/component-api 感兴趣,它定义了几个用于访问 API 的 JavaScript 程序。

用法

use Skyline\API\Controller\AbstractAPIActionController;
use Symfony\Component\HttpFoundation\Request;
use Skyline\Kernel\Service\CORSService;

class MyAPIActionController extends AbstractAPIActionController {
    // Routing to the action is done by default in the
    // routing configuration or annotation compiler
    public function myAction() {
        // ...
    }
    
    // But this action gets only performed if ...
    public function acceptsAnonymousRequest(Request $request): bool
    {
        // ... the request has an origin header field or
        return SkyGetRunModes() > SKY_RUNMODE_PRODUCTION;
    }
    
    public function acceptsCrossOriginRequest(Request $request): bool
    {
        // ... the request came from the same origin or
        return CORSService::isRegistered( $request->getHost() );
    }
    
    public function acceptOrigin(Request $request, bool &$requireCredentials = false): bool
    {
        // ... the request is cross origin, decide to accept it generally or specified.
        // Also declare, if the request must identify itself.
        return CORSService::getAllowedOriginOf($request, $requireCredentials) ? true : false;
    }

    // By version 0.8.5 this method must decide if the request must be verified.
    // This method gets called right before the main action is handled.
    protected function enableCsrfCheck(Request $request): bool {
    	if(strcasecmp($request->getMethod(), 'GET') === false)
    		return true;
        return false;
    }
}