lkt/http-response 问题修复 & 功能扩展

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

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

lkt/http-response

最新稳定版本:2.0.10

Composer 安装命令:

composer require lkt/http-response

包简介

LKT HTTP Response

README 文档

README

Installation

composer require lkt/http-response

Usage

Instantiate a new response instance with the generic constructor:

use Lkt\Http\Response;

Response::status(200, ['some' => 'data']);

Or you can instantiate it with all the more specific constructors:

use Lkt\Http\Response;

Response::ok(['some' => 'data']); // Same as: Response::status(200, ['some' => 'data']);

Constructor list

Method Status code
::ok 200
::created 201
::accepted 202
::noContent 204
::multipleChoices 300
::movedPermanently 301
::found 302
::seeOther 303
::notModified 304
::badRequest 400
::unauthorized 401
::forbidden 403
::notFound 404
::methodNotAllowed 405
::internalServerError 500
::notImplemented 501
::badGateway 502
::serviceUnavailable 503

Configure HTTP headers

The Response instance can handle some http header.

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(); 

Default content type

The default content type for Response is JSON.

Sending text/html

When using a text/html response, simply pass the string as an argument. By default, this will turn the response content type to text/html.

use Lkt\Http\Response;
$response = Response::ok('may the force be with you');

// Output content
$response->sendContent();

Sending files

You can send a file in a similar way, only remember to refresh the MIME type

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();

Supported file extensions

Response implements lkt/mime to check file extensions (see supported MIME).

Examples

Sending a valid JSON response

use Lkt\Http\Response;

return Response::ok(['some' => 'data']);

Sending a valid text/html response

use Lkt\Http\Response;

return Response::ok('<p>Hello world!</p>');

Sending a forbidden response

use Lkt\Http\Response;

return Response::forbidden(); // Empty
return Response::forbidden(['some' => 'data']); // JSON info
return Response::forbidden('Forbidden'); // text/html info

Sending a file

use Lkt\Http\Response;

return Response::ok(file_get_contents($pathToImage))
    ->setContentTypeByFileExtension('jpg')
    ->setLastModifiedHeader(filemtime($pathToImage));

Downloading a file

use Lkt\Http\Response;

return Response::ok(file_get_contents($pathToImage))
    ->setContentTypeByFileExtension('jpg')
    ->setContentDispositionAttachment('image.jpg')
    ->setLastModifiedHeader(filemtime($pathToImage));

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-07