makinacorpus/static-passthrough-bundle

通过 Symfony 轻松提供静态文件树

0.1.14 2024-08-27 07:34 UTC

README

此扩展包是为了满足以下简单用例而开发的:通过 Symfony 应用程序提供静态文件(例如生成的文档)。

入门指南

1/ 使用 Composer 安装

composer require makinacorpus/static-passthrough-bundle

2/ 注册扩展包

# config/bundles.php
return [

    // ...

    MakinaCorpus\StaticPassthroughBundle\StaticPassthroughBundle::class => ['all' => true],

    // ...
];

3/ 添加静态文件透传路由定义

# config/routes.yaml

# ...

static_passthrough:
    resource: "@StaticPassthroughBundle/Resources/config/routes.php"

# ...

4/ 配置静态文件透传

假设我们有一个位于应用程序根目录的 docs 文件夹,其中包含一个简单的 test.html 文件:这是我们想要通过我们的 Symfony 应用程序提供的静态文件。

为此,按照以下方式配置扩展包

# config/package/static_passthrough.yaml
static_passthrough:
  definitions:
    docs: # Route name will be 'static_passthrough_docs'
      root_folder: 'docs' # Where to find files to passthrough (this path has to be relative to %kernel.project_dir%)
      path_prefix: 'docs/' # Path to reach files in root_folder

别忘了清除缓存

bin/console c:c

5/ 使用浏览器访问文件

访问 [app_basepath]/docs/test.html,你应该能看到你的 HTML 文件。

请注意,你也可以通过访问 [app_basepath]/docs/test 来查看它,事实上,当你尝试访问 [app_basepath]/docs/test 时,扩展包将按照以下顺序在这些不同路径中查找文件

  • [app_basepath]/docs/test
  • [app_basepath]/docs/test.html
  • [app_basepath]/docs/test/index.html

6/ 生成 URL

以下是如何创建一个 URL 以访问上述 'test.html' 文件的示例

从控制器中

$this->generateUrl(
    'static_passthrough_docs',
    ['path' => 'test.html]
);

从 Twig 中

{{ path('static_passthrough_docs', {'path': 'test.html'}) }}