espresso / app
Requires
- php: >=7.2
- espresso/http-module: ^3.1
- http-interop/response-sender: ^1.0
- middlewares/payload: ^2.1
- middlewares/whoops: ^1.2
- nyholm/psr7: ^1.1
- nyholm/psr7-server: ^0.3.0
Requires (Dev)
- codacy/coverage: ^1.4
- friendsofphp/php-cs-fixer: ^2.15
- league/container: ^3.3
- phpunit/phpunit: 7.5
README
PHP 咖啡时光
简介
Espresso 是一个基于 PHP 的 HTTP 微型框架,为您的 Web 应用程序提供支持。它深受 Node 的 Express 启发。
我们的目标是使 PHP 开发像编写 Node Js 应用程序一样简单,同时使用 PHP 优秀的面向对象特性、最佳模式和 FIG 标准,以实现最大程度的互操作性。
安装
要安装 Espresso,只需运行
composer require espresso/app
或者,如果您更喜欢具有一些目录结构的空项目,可以使用
composer create-project espresso/skeleton <folder>
请参阅空项目文档以了解如何使用它。
注意:Espresso 是一个带有一些工具的 HTTP 框架,这些工具可以简化您的开发工作。如果您不需要所有这些工具,您可能只需要使用
espresso/http-module
,这是 Espresso 的基础类。
快速入门
启动 Espresso 简单。要创建一个“你好,世界”应用程序,只需在公共目录中创建您的入口控制器(index.php
),然后编写
<?php use Espresso\App\Espresso; use Zend\Diactoros\Response\HtmlResponse; // We create the application $app = Espresso::createApp(); // We define a single get route with a callable handler $app->get('/', static function () { return new HtmlResponse('Hello World'); }); // We run the app, processing the request and emitting a response. $app->run();
架构概述
Espresso 的关键是 中间件。如果您不了解中间件是什么以及它是如何工作的,那么使用 Espresso 将会有困难。我建议您阅读以下资源以了解更多关于该模式的信息
- Slim 框架关于中间件是什么的文档
- Matthew Weier O'Phinney 在中间件之前的 PHP 生态系统状态
- Phil Sturgeon 关于中间件为什么重要的看法
- Anthony Ferrara 关于实现中间件的最佳方法的看法
在 Espresso 中,一切都是中间件(路由、路径、处理器、执行管道等)。甚至主要的 Espresso 实例本身也是一个中间件。一个结构良好的 Espresso 应用程序不过是一棵中间件的树。Espresso 的理念是您不需要手动构建这棵树,而是可以使用 Espresso 提供的简单 API,这与 Express JS 的 API 非常相似。
使用 Espresso
您可以在 Espresso 中执行两个主要操作
- 添加中间件
- 注册路由
注意:实际上,Espresso 只执行一个操作,那就是将中间件追加到中间件队列中。路由以及路径本身也是中间件。
添加中间件
要添加中间件,必须在 Espresso 实例上调用 use
方法。
例如,要添加一个添加响应头 X-Powered-By
的中间件。
<?php use Psr\Http\Message\ServerRequestInterface as Req; use Psr\Http\Message\ResponseInterface as Res; use Psr\Http\Server\RequestHandlerInterface as Next; $app->use(static function (Req $req, Next $next): Res { return $next->handle($req) ->withAddedHeader('X-Powered-By', 'Espresso'); });
注册路由
为了注册路由,您可以使用 get
、post
、put
、patch
、delete
和 route
方法。所有这些方法都需要一个路由路径和一个或多个处理器。
<?php use Psr\Http\Message\ServerRequestInterface as Req; use Psr\Http\Message\ResponseInterface as Res; use Zend\Diactoros\Response\JsonResponse; $app->get('/users/:id', static function (Req $req): Res { // We get the user id from the request $userId = $req->getAttribute('id'); return new JsonResponse(['userId' => $userId]); });
函数调用支持的类型
如您在示例中看到的,我们一直在将这些方法传递闭包。但这对于大型应用程序来说并不方便。理想情况下,我们需要将这些逻辑封装在它们自己的类中。
因此,在 Espresso 中,您可以传递 psr-15 中间件实例而不是闭包。
让我们使用类来实现前面的相同示例。
<?php use Psr\Http\Message\ServerRequestInterface as Req; use Psr\Http\Message\ResponseInterface as Res; use Zend\Diactoros\Response\JsonResponse; use Psr\Http\Server\RequestHandlerInterface as Next; use Psr\Http\Server\MiddlewareInterface; class UserIdHandler implements MiddlewareInterface { public function process(Req $req, Next $next) : Res { $userId = $req->getAttribute('id'); return new JsonResponse(['userId' => $userId]); } } $app->get('/users/:id', new UserIdHandler());
中间件解析器
由于 Espresso 的 MiddlewareResolver
,您不仅可以注册对象和闭包,还可以注册更多类型的参数。
中间件解析器是一个接口,其主要目的是接收任何类型的参数,并返回一个实现 MiddlewareInterface
的对象。
Espresso 使用 默认的 espresso/http-module
实现,它可以解析以下内容:
- 数组:将每个元素递归解析并放入一个
EspressoPipeline
,这是一个执行一系列中间件的中间件。 - 中间件实例:直接返回它。
- 请求处理器实例:使用 请求处理器中间件 进行装饰。
- 闭包:使用 可调用中间件 进行装饰,并可选择将对象绑定到
$this
。 - 字符串:如果您提供了 DI 容器和具有该字符串的服务,则创建一个包装该服务的 延迟中间件。
- 包含 @ 的字符串:将部分拆分,然后找到一个服务并在其上调用一个方法。这样您就可以使用控制器模式。
使用 DI 容器
Espresso 可以使用 DI 容器解析服务名称。只需将其传递给工厂方法,它将解析遇到的每个服务。
Espresso 附带了一个为 league/container
准备好的 Service Provider,这是骨架的首选。