timostamm / web-resource
PHP 网络资源
v1.2.1
2023-10-08 14:09 UTC
Requires
- php: ^7.2 || ^8.0
- symfony/http-foundation: >=4.4 < 6
- symfony/mime: >=4.4 < 6
Requires (Dev)
- ext-zip: *
- mikey179/vfsstream: ^1.6.11
- phpunit/phpunit: ^8.5.23 || ^9
- symfony/process: >=4.4 < 6
README
一组代表资源的类集合,适用于发送到 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