首页
/ FileServeBundle 技术文档

FileServeBundle 技术文档

2024-12-25 02:41:52作者:卓艾滢Kingsley

1. 安装指南

首先,您需要在您的 composer.json 文件中添加以下依赖项:

{
    "require": {
        "igorw/file-serve-bundle": "~1.0"
    }
}

接下来,将 FileServeBundle 添加到您的应用程序的内核中:

public function registerBundles()
{
    $bundles = array(
        // ...
        new Igorw\FileServeBundle\IgorwFileServeBundle(),
        // ...
    );
    // ...
}

2. 项目的使用说明

使用 igorw_file_serve.response_factory 服务来创建 FileServe 响应。默认情况下,路径是相对于 app 目录的。

$response = $this->get('igorw_file_serve.response_factory')->create('../VERSION', 'text/plain');

您还可以向 create 方法的第三个参数传递一组选项。

$options = array(
    'serve_filename' => 'VERSION.txt',
    'absolute_path' => true,
    'inline' => false,
);

$response = $this->get('igorw_file_serve.response_factory')
    ->create('../VERSION', 'text/plain', $options);
  • serve_filename: 浏览器下载文件时使用的文件名。
  • absolute_path: 如果启用,该捆绑包将忽略 base_dir 选项,并使用提供的文件名作为绝对路径。

您可以配置工厂的使用,例如使用 nginx XSendfile 响应工厂:

igorw_file_serve:
    factory: sendfile     # 默认值是 "php"

您还可以配置基本目录:

igorw_file_serve:
    base_dir: /files/dir     # 默认值是 "%kernel.root_dir%"

默认情况下,此捆绑包在创建响应对象时执行 file_exists 检查。最新的 nginx 版本需要相对路径,在这种情况下,PHP 中的路径不是实际的物理路径。使用 skip_file_exists 设置来禁用检查。

igorw_file_serve:
    skip_file_exists: true  # 默认值是 false

支持的工厂

  • php
  • sendfile (nginx)
  • xsendfile (apache)

3. 项目API使用文档

本项目API使用方法已在上文使用说明中详细阐述,具体请参考“项目的使用说明”一节。

4. 项目安装方式

项目的安装方式在“安装指南”一节中已经详细说明,请按照该节的步骤进行安装。

登录后查看全文
热门项目推荐