geekcow/fony-core
Composer 安装命令:
composer require geekcow/fony-core
包简介
Core classes for fony PHP API
关键字:
README 文档
README
Core classes for Fony PHP API Framework
Usage
You can add fony core as a dependency of your API by declaring a base API class like the following example:
<?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())); } ?>
Your Router class should be an implementation of the FonyApi class provided here.
How does the Router works
The way the Router class works comes as following
- The constructor sweeps over the endpoint requested. So an action class (aka controller) can be prestaged.
- The endpoint will execute a method called the with same name of the request. ie. If the endpoint is
/api/user/:idthe endpoint we are going to execute isuser, so auser()function needs to be defined. - Additionally to it, if the endpoint is written with dashes, then the endpoint should be written with camel case.
This is an example of a Router class:
<?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(); } }
Session handling
By default, Fony uses Oauth2 authentication, so it relies on the configuration file required by installing Fony:
- List of parameters TBD
Alternatively you can define your own authentication mechanism (like the fony-auth project), where you can create an Authentication class as an implementation of the AuthenticatorInterface and initialize it in the Router constructor:
$sessionInstance = SessionUtils::getInstance(new CustomAuthenticatorClass());
Example
Soon an example repository will be available
geekcow/fony-core 适用场景与选型建议
geekcow/fony-core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 136 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「fony」 「fony-core」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 geekcow/fony-core 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 geekcow/fony-core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 geekcow/fony-core 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Fony PHP API framework installer
Authentication module for Fony PHP
A lightweight plain-PHP framework for database-backed CRUD APIs.
Modern, strongly-typed, PSR-compliant PHP client for the WeFact v2 API.
PHP SDK for the Enconvert file conversion API
统计信息
- 总下载量: 136
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-02-17