lkt / http-response
LKT HTTP 响应
2.0.8
2023-08-18 15:20 UTC
Requires
- php: >=8.1.0
- lkt/mime: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.2
README
安装
composer require lkt/http-response
用法
使用通用构造函数实例化新的响应实例
use Lkt\Http\Response; Response::status(200, ['some' => 'data']);
或者您可以使用所有更具体的构造函数实例化它
use Lkt\Http\Response; Response::ok(['some' => 'data']); // Same as: Response::status(200, ['some' => 'data']);
构造函数列表
配置 HTTP 标头
Response
实例可以处理一些 HTTP 标头。
use Lkt\Http\Response; $response = Response::ok(['hey' => 'how are you?']); // Set content type to JSON (default Content Type) $response->setContentTypeJSON(); // Or to text/html $response->setContentTypeTextHTML(); // Also, you can set the expiration and max age: $response->setCacheControlMaxAgeHeaderToOneYear(84600); $response->setExpiresHeader(84600); // Or use the shortcuts: $response->setExpiresHeaderToOneDay(); $response->setExpiresHeaderToOneWeek(); $response->setExpiresHeaderToOneMonth(); $response->setExpiresHeaderToOneYear(); $response->setCacheControlMaxAgeHeaderToOneDay(); $response->setCacheControlMaxAgeHeaderToOneWeek(); $response->setCacheControlMaxAgeHeaderToOneMonth(); $response->setCacheControlMaxAgeHeaderToOneYear(); // Send the response $response->sendContent();
默认内容类型
Response
的默认内容类型是 JSON。
发送 text/html
当使用 text/html 响应时,只需将字符串作为参数传递。默认情况下,这将把响应内容类型设置为 text/html
。
use Lkt\Http\Response; $response = Response::ok('may the force be with you'); // Output content $response->sendContent();
发送文件
您可以使用类似的方式发送文件,只需记住刷新 MIME 类型
use Lkt\Http\Response; // Get a string with the content of the file $content = file_get_contents($pathToImage); // Create a response $response = Response::ok($content); // Set the MIME type for the file // Automatically detect the mime type from file extension // Notice: If the extension wasn't detected, the response will turn into an octet-stream $response->setContentTypeByFileExtension('jpg'); // It can be pdf, png, doc, docx, csv, ... // Set the last modified header $lastModified = filemtime($pathToImage); $response->setLastModifiedHeader($lastModified); // Turn it to download $response->setContentDispositionAttachment('image.jpg'); // Output img content $response->sendContent();
支持的文件扩展名
Response
实现了 lkt/mime 来检查文件扩展名 (查看支持的 MIME)。
示例
发送有效的 JSON 响应
use Lkt\Http\Response; return Response::ok(['some' => 'data']);
发送有效的 text/html 响应
use Lkt\Http\Response; return Response::ok('<p>Hello world!</p>');
发送禁止的响应
use Lkt\Http\Response; return Response::forbidden(); // Empty return Response::forbidden(['some' => 'data']); // JSON info return Response::forbidden('Forbidden'); // text/html info
发送文件
use Lkt\Http\Response; return Response::ok(file_get_contents($pathToImage)) ->setContentTypeByFileExtension('jpg') ->setLastModifiedHeader(filemtime($pathToImage));
下载文件
use Lkt\Http\Response; return Response::ok(file_get_contents($pathToImage)) ->setContentTypeByFileExtension('jpg') ->setContentDispositionAttachment('image.jpg') ->setLastModifiedHeader(filemtime($pathToImage));