nddcoder/rest

Rest 框架。

安装: 6

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

语言:Shell

类型:项目

0.0.3 2021-08-30 09:40 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:49 UTC


README

Rest 框架 - 基于 ReactPHP 库的 PHP 框架

文档

安装

服务器需求

Rest 框架有一些系统需求。

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

安装 Rest

Rest 使用 Composer 来管理其依赖项。因此,在使用 Rest 之前,请确保您已经在您的机器上安装了 Composer。

通过在终端中运行 Composer 的 create-project 命令来安装 Rest

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

本地开发服务器

只需运行 index.php 文件

php index.php

或者,如果您已安装 nodemon,您可以使用 nodemon 获得自动重新加载功能

nodemon index.php

部署

使用 Docker

只需基于项目的 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> 

使用 Supervisor

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

用法

路由器

Rest 使用 Fast Route 进行路由。应用程序路由可以注册在 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(...)
        */
    }
}

控制器

控制器方法的第一个参数始终是 ServerRequestInterface,任何路由参数都将跟在此之后。

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";
    }
}

依赖注入

将类绑定到容器

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

单例

要使一个类成为单例,您可以使其实现 Singleton 接口或使用 singleton 方法将其绑定到容器

use Rest\Contracts\Singleton;

class SlackService extends Singleton {

}

//or

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

从容器中解析实例

__construct 函数中注入依赖项

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

使用 app 辅助工具

__construct 函数中注入依赖项

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

辅助工具

app:返回 Application 实例或从容器中解析实例

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

view:返回 ViewResponse 实例。接受视图名称和可选需要传递给视图的数据

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

response:返回 Response 实例,用于构建响应

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

env:从 $_ENV 获取环境变量。Rest 使用 vlucas/phpdotenv.env 文件中加载环境变量。

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

dd:用于调试的 var_dump and die 变量。

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

abort:返回中间 http 响应。接受 status codemessage

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

abort_if:基于 $conditionabort

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

abort_unlessabort_if 的反面

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

logger:将字符串或 Throwable 实例记录到控制台

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