承接 rozbehsharahi/fhp-rest-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rozbehsharahi/fhp-rest-api

Composer 安装命令:

composer require rozbehsharahi/fhp-rest-api

包简介

Super quick setup of a PHP-REST-API without the use of MySQL, Apache, ...

README 文档

README

Description

FHP REST API provides an easy way to setup a JSON FILE based REST API.

There are no dependencies to MySQL or other databases. All files will be stored as json files by default. Still you may also integrate your own way of saving data, by integrating your own controllers, entites and normalizers.

I'm trying really hard to make this very flexible and at the same easy to understand, i hope you enjoy working with this.

Installation

$ composer require rozbehsharahi/fhp-rest-api

How to

The following index.php is enough to provide PUT, GET, POST, OPTION, DELETE routes for your application on the /posts route:

<?php
use Fhp\Rest\Api;
use Fhp\Rest\Repository\JsonRepository;
use Fhp\Rest\Controller\FlexEntityController;

require_once('vendor/autoload.php');

// Set json database folder
JsonRepository::setDirectory(__DIR__ . '/database/');

// Api call here
Api::create()
    ->activateEntity('post', FlexEntityController::class) // <-- use singular entity name
    ->run();

That's it!

How to use entity classes

(Still experimental)

This way of using FHP REST API is still very experimental, so please used this with caution. For instance you have to define fhp property type annotations, though they don't have any impact on the way a property is saved yet.

<?php
use Fhp\Rest\Api;
use Fhp\Rest\Repository\JsonRepository;

require_once('vendor/autoload.php');

// Set json database folder
JsonRepository::setDirectory(__DIR__ . '/database/');

// Api call here
Api::create()
    ->activateEntity(My\Example\Page::class)
    ->run();

Your entity class should look like this:

<?php
namespace My\Example;

/**
 * My Entity class
 *
 * @class Page
 */
class Page
{

    /**
     * @Fhp\Rest\PropertyType\StringType
     */
    protected $id;

    /**
     * @Fhp\Rest\PropertyType\StringType
     */
    protected $url;

    public function getId() { return $this->id; }

    public function setId($id) { $this->id = $id; return $this; }

    public function getUrl() { return $this->url; }

    public function setUrl($url) { $this->url = $url; return $this; }
}

At the moment it does not make any difference which property type you will use. But it is a good preparation for next releases. You will still have to annotate with one of the following annotations:

  • @Fhp\Rest\PropertyType\StringType
  • @Fhp\Rest\PropertyType\BooleanType
  • @Fhp\Rest\PropertyType\IntegerType

Defining custom entity-controllers

You may also define your own controllers to handle model-requests. Just pass your controller's class name as the third parameter to $api->createModel.

Please extend Fhp\Rest\Controller and feel free to override the basic actions:

  • public function indexAction($request, $response, $args) {}
  • public function showAction($request, $response, $args) {}
  • public function updateAction($request, $response, $args) {}
  • public function deleteAction($request, $response, $args) {}
  • public function createAction($request, $response, $args) {}

by calling $api->activateEntity like following:

$api->activateEntity(
    \My\Entity\Post::class,
    \My\Own\PostController::class
);

Super fast start without Apache

No apache, No MySQL, just make sure PHP is installed on your machine.

Since FHP REST API is super independent you may start your server on almost any machine that supports PHP7+ (in future also PHP5.6+). Not even Apache is needed to get it started. Just change your directory to the projects root and go on with:

$ php -S localhost:8000

Start with Apache

In case you want to start FHP REST API on an apache server, please add the following .htaccess file to your project's root where index.php is located.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Security issues / CORS

FHP REST API has a set of default-headers that are defined in Fhp\Rest\Api. These headers also contain 'Access-Control-Allow-Origin' => '*' which is a security issue depending on the project.

Please make sure to configure your own header-settings when going to production.

You may do this by following:

$api->setHeaders([
    'Content-Type' => 'application/json',
    'Access-Control-Allow-Origin' => 'http://my-specific-doain.com'
]);

Todos and issues

  • PropertyTypes
  • Refactoring

rozbehsharahi/fhp-rest-api 适用场景与选型建议

rozbehsharahi/fhp-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「file」 「rest」 「restful」 「Simple」 「ember」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rozbehsharahi/fhp-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rozbehsharahi/fhp-rest-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 rozbehsharahi/fhp-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-24