elephox/miniphox-framework 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

elephox/miniphox-framework

最新稳定版本:v0.8.8

Composer 安装命令:

composer require elephox/miniphox-framework

包简介

A minimal API framework based on Elephox.

README 文档

README

TL;DR:

composer create-project elephox/miniphox my-api && \
cd my-api && \
composer run serve

Then:

curl http://localhost:8008/api/greet/$(whoami)

Done.

Miniphox explained as fast as possible

  • React HTTP server backbone (multi-threaded PHP socket server)
  • Elephox/DI dependency injection
  • Approachable router using PHP Attributes
  • Best for (small) API projects
  • Fast to production with minimal effort

Show me some code already

hello-world.php

<?php
declare(strict_types=1);

// this is the default namespace; can be specified in build()
namespace App;

require_once dirname(__DIR__) . '/vendor/autoload.php';

use Elephox\Miniphox\Attributes\Get;
use Elephox\Miniphox\MiniphoxBase;

#[Get] // defaults to '/'
function index(): string {
    return "Hello, World!";
}

#[Get('/greet/[name]')] // using route params
function greet(string $name): string {
    return "Hello, $name!";
}

// This creates a new Miniphox app.
MiniphoxBase::build()

    // index() and greet() are mounted at '/api'.
    // This maps '/api' -> index() and '/api/greet/[name]' -> greet() according to their attributes above.
    //
    // You can pass first-class-callables or just the method name to the mount method.
    ->mount('/api', index(...), 'greet')

    // This will start the HTTP server on http://0.0.0.0:8008.
    // Pass a string uri to bind to a specific ip address or a different port.
    ->run();

The example above is the simplest way to use Miniphox. Just some straightforward route handlers with little to no logic and one line to run everything.

A more sophisticated example might use dependency injection to inject a database connection or logger into the route handler:

counter.php

<?php // [...]

#[Get('/count')]
function counter(stdClass $counter): string {
    // $counter will get injected from the service specified below

    return "The current count is $counter->i";
}

$app = Miniphox::build()->mount('/api', counter(...));

// transient services get created every time they are requested (unlike singletons)
$app->services->addTransient(stdClass::class, stdClass::class, function () {
    static $i; // this keeps track of how many times this services was created

    $counter = new stdClass();
    $counter->i = ++$i;

    return $counter;
});

$app->run();

You can find both of these examples in the folder example. Along with them is a showcase.php file, which contains some advanced examples.

The ugly part

This project is still in its infant state, and it might stay that way. There are some things left to do, namely improving the routers capabilities, and I don't know when or how much time I'm going to invest going forward. It is a toy project after all.

Some things left TODO include:

  • check if multiple dynamic routes exist and determine best fit
  • regex patterns in route parameters
  • check how to improve performance even further (is it possible to leverage opcache?)
  • write up some tutorials on how to set up Doctrine and other common software stacks (phpdotenv using Elephox/Configuration?)
  • Improve the way server timings are logged (maybe also look into how to send headers while processing the request with ReactPHP)
  • Add file watching and auto-restarting server
  • MAYBE: refactor Miniphox to be server-agnostic (exchange ReactPHP with other servers like OpenSwoole, Amphp?)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固