定制 tomlerendu/microapi 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tomlerendu/microapi

Composer 安装命令:

composer require tomlerendu/microapi

包简介

An extremely lightweight PHP framework for creating APIs

README 文档

README

MicroAPI is an extremely lightweight PHP framework for creating REST APIs.

Requirements

  • Apache 2.0+
    • mod_rewrite enabled
  • PHP 5.4+

Downloading and Installing

Structure

File system

A MicroAPI application by default has the following structure.

  • App
    • Controller
    • Models
    • Services
  • MicroAPI
  • public

The directory structure can be changed by editing the constants MICROAPI_PATH and APP_PATH in public/const.php then moving the directories to their corresponding locations. It is advisable to keep application code out of the public directory.

The App directory is where you write your application.

The MicroAPI directory contains the framework. If there are multiple websites using MicroAPI on the same system they can be configured to share one copy of it.

The public directory is where the requests enter the framework.

Namespaces and autoloading

MicroAPI follows the PSR-4 standard for autoloading classes.

Request response cycle

Within the App directory there are three files

  1. App/config.php function is executed
  2. App/run.php function is executed
  3. App/routes.php function is executed - If a route is matched its controller is executed

Config

Configuration variables for the application can be defined in App/config.php. By default there are options for the database and response already listed. It's recommended not to define values that have a key starting with microapi..

Routing

Routes for your application are defined in the app/routes.php file. The router will initialise the controller for the first match, once a match has been found the rest of the routes will not be checked.

By default the app/routes.php has two sample routes showing how to setup a function and a class.

return function($router)
{

    $router->get([
        'route' => '/name/(name)',
        'class' => 'Example@getName',
        'require' => function() {
            return 1 + 1 == 2;
        }
    ]);

    $router->post([
        'route' => '/name/(name)',
        'function' => 'functionExample@postName',
    ]);

};

Methods

The router has the methods get, post, put, delete which match the corresponding HTTP methods and any which will match any HTTP method.

Routes and wildcards

Extra requirements

The require function allows for any additional requirements to be specified before the route is matched. If anything other than true is returned from the function the route will not be matched.

$router->get([
    'route' => '/user/(id)',
    'function' => 'user@details',
    'require' => function($database, $request) {
        $user = $database->select('SELECT id FROM User WHERE id = ?', $request->getPathWildcard('id'));
        return count($user) === 1;
    }
]);

Controllers

Controllers are dependency injected, they can be either a function or a method on an object.

function myController($request, $response, $database)
{
    $id = $request->getParam('id');
    $results = $database->select('SELECT * FROM Table WHERE id = ?', $id);

    $response->make($results);
}

Services

A service is an object that can be injected at various places in the application, there will only one instance of each service per request. By default MicroAPI comes with six services.

Request

The request object is a representation of the request the user made.

Responses

Responses are "replies" to a clients request, only one response should be sent per request.

Database

The database service is built on top of PDO, it has four convenience functions to make accessing the database easier. For more specific tasks you can access the database directly by calling getConnection() which returns the PDO object.

Injector

Autoloader

Config

Creating a custom service

Models

Known issues

tomlerendu/microapi 适用场景与选型建议

tomlerendu/microapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 tomlerendu/microapi 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-01-01