timostamm/web-resource

PHP 网络资源

v1.2.1 2023-10-08 14:09 UTC

This package is auto-updated.

Last update: 2024-09-08 19:37:17 UTC


README

build Packagist PHP Version GitHub tag License

一组代表资源的类集合,适用于发送到 HTTP 客户端。

这些类的优点是提供了一个公共接口,它将数据流与一系列元数据(如 MIME 类型、修改日期等)捆绑在一起。

这种抽象使得缓存、延迟处理、媒体类型转换和其他管道操作比处理文件更加方便。

示例

// In a Symfony Controller:
$res = Resource::fromFile('dir/my-file.txt');
$res->getMimetype(); // guessed mimetype
return new ResourceResponse($res); 

// Fetch a resource from an URL:
$res = Resource::fromUrl('http://foo.bar/my-file.txt');
$res->getMimetype(); // mimetype from server
$res->getLastModified(); // modification date from server

// Overriding a mimetype and the file name:
$res = Resource::fromFile('dir/my-file.txt', [
	'mimetype' => 'applicatio/octet-stream', 
	'filename' => 'text.txt'
]);
return new ResourceResponse($res); // will also set the appropriate content-disposition header