ShrooPHP构建PSR兼容应用的库。

v1.3.1 2018-11-13 22:58 UTC

This package is auto-updated.

Last update: 2024-09-08 22:30:21 UTC


README

ShrooPHP构建PSR兼容应用的库。

安装说明

composer require 'shroophp/psr ^1.0'

示例用法

<?php

use GuzzleHttp\Psr7\ServerRequest;
use ShrooPHP\Core\Request;
use ShrooPHP\Framework\Application;
use ShrooPHP\Framework\Request\Responses\Response;
use ShrooPHP\PSR\RequestHandler;

require 'vendor/autoload.php';

$default = new \GuzzleHttp\Psr7\Response(Response::HTTP_NOT_FOUND);
$handler = new RequestHandler($default);

$handler->apply(function (Application $app) {

	$app->path('/greeting', $app->_(function (Request $request) {

		return Response::string('Hello, world!', 'text/plain');
	}));
});

$no = $handler->handle(new ServerRequest('GET', '/'));         // 404 Not Found
$ok = $handler->handle(new ServerRequest('GET', '/greeting')); // 200 OK

重要注意事项

当将应用修改器应用于请求处理器时,重要的是将每个响应间接指定为回调的返回值(而不是将响应作为显式应用修改器或处理器的响应)。

这是因为直接修改器和处理器没有机会使用与应用程序关联的表示器。从请求处理器的角度来看,这意味着响应无法被缓冲并转换为PSR兼容的实例。