nddcoder/rest 问题修复 & 功能扩展

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

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

nddcoder/rest

Composer 安装命令:

composer create-project nddcoder/rest

包简介

The Rest Framework.

README 文档

README

Rest Framework - PHP Framework for ReactPHP Library

Docs

Installation

Server Requirements

The Rest framework has a few system requirements.

  • PHP >= 7.4
  • BCMath PHP Extension
  • Ctype PHP Extension
  • Fileinfo PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Installing Rest

Rest utilizes Composer to manage its dependencies. So, before using Rest, make sure you have Composer installed on your machine.

Install Rest by issuing the Composer create-project command in your terminal:

composer create-project --prefer-dist nddcoder/rest blog

Local Development Server

Just run index.php file

php index.php

Or, if you have nodemon installed, you can have auto reload feature by using nodemon

nodemon index.php

Deploy

Using Docker

Just build image based on project's Dockerfile

docker build -t <image_name>:<image_tag> .
docker run -d \
           -p 8080:8080 \
           -e APP_PORT="0.0.0.0:8080" \
           <image_name>:<image_tag> 

Using Supervisor

[program:app-name]
process_name=%(program_name)s_%(process_num)02d
command=php /paht/to/project/index.php
autostart=true
autorestart=true

Usage

Router

Rest using Fast Route for routing. Application routes can be register in app/Router.php.

class Router extends BaseRouter
{
    protected function register(RouteCollector $routes): void
    {
        $routes->get('/', HomeController::class); //using invokeable controller
        $routes->get('/home', [HomeController::class, 'home']);
        $routes->get('/hello/{name}', [HomeController::class, 'hello']);
        
        /*
        $routes->post(...)
        $routes->put(...)
        $routes->delete(...)
        */
    }
}

Controller

The first parameter of controller method always is ServerRequestInterface, any route params will following this.

class HomeController
{
    public function __invoke(ServerRequestInterface $request)
    {
        $frameworkVersion = Application::VERSION;
        return view('home.twig', compact('frameworkVersion'));
    }

    public function home()
    {
        return response()->redirect('/');
    }
    
    public function hello(ServerRequestInterface $request, $name)
    {
        return "Hello $name";
    }
}

Dependency Injection

Bind class to container

Application::getInstance()
    ->onBoot(function (Application $app) {
        $app->bind(ProductServiceInterface::class, fn($app) => new ProductServiceImpl());
    });

Singleton

To make a class is singleton, you can make class implemnt Singleton interface or bind to container using singleton method

use Rest\Contracts\Singleton;

class SlackService extends Singleton {

}

//or

Application::getInstance()
    ->onBoot(function (Application $app) {
        $app->singleton(SlackService::class, SlackService::class);
    });

Resolve an instance from container

Inject dependencies in __construct function

class ProductController
{
    protected ProductServiceInterface $productService;
    
    public function __construct(ProductServiceInterface $service)
    {
        $this->productService = $service;
    }
}

Using app helper

Inject dependencies in __construct function

class ProductController
{
    public function __construct()
    {
        $this->productService = app(ProductServiceInterface::class);
        //or
        $this->productService = app()->make(ProductServiceInterface::class);
    }
}

Helpers

app: return Application instance or resolve an instance from container

$application = app(); //return Application instance
$classInstance = app(ClassName::class); //ClassName instance

view: return ViewResponse instance. Accept view name and optional data need pass to view

class ProductController
{
    public function index()
    {
        $products = [
            //...
        ];
        
        return view('products.index', [
            'products' => $products
        ]);
    }
}

response: return Response instance, use to build response

class ProductController
{
    public function index()
    {
        $products = [
            //...
        ];
        
        return response()->json([
           'data' => $products
        ]);
    }
}

env: get enviroment vairable from $_ENV. Rest using vlucas/phpdotenv for load env variables from .env file.

$debug = env('APP_DEBUG') == 'true';

dd: var_dump and die variable for debugging.

dd($var1, $var2, $var3);

abort: intermediately return http response. Accept status code and message

abort(500);
abort(403, 'Permission Denied');

abort_if: abort based on $condition

abort_if($condition, $status, $messagge);

abort_unless: reversed side of abort_if

abort_unless($condition, $status, $messagge);

logger: log a string or an Throwable instance to console

logger('Error occurred');
logger($exeption);

nddcoder/rest 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: Shell

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-20