makinacorpus/static-passthrough-bundle

Easily serve a tree of statics files through Symfony

Installs: 31 846

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 15

Forks: 3

Open Issues: 1

Type:symfony-bundle

0.1.12 2023-11-21 12:52 UTC

README

This bundle has been developped for a simple use case : serve a built documentation through a symfony application.

Getting started

1/ Get sources with Composer:

composer require makinacorpus/static-passthrough-bundle

2/ Register the bundle

# config/bundles.php
return [

    // ...

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

    // ...
];

3/ Add Static Passthrough Routes definition

# config/routes.yaml

# ...

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

# ...

4/ Configure Static Passthrough

Let's assume we have a docs folder in the root directory of our application, with a simple test.html file in it: that's the static file we want to serve through our Symfony app.

To do so, configure the bundle like this:

# 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

Don't forget to clear the cache:

bin/console c:c

5/ Reach the file with your browser :

Visit [app_basepath]/docs/test.html, you should see your HTML file appear.

Note that you could also view it visiting [app_basepath]/docs/test, in fact when you try to visit [app_basepath]/docs/test, the bundle will look for a file in these different paths, in that order:

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

6/ Generate URL

Here is an examples to create an URL to reach 'test.html' file described above:

From a controller:

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

From twig:

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