Static and uploaded files
The StaticFilesMiddleware
and UploadedFilesMiddleware
provide a way of serving
static content and user uploaded files.
There are two separate middlewares to allow distinct handling in the middleware pipeline. For example, you could set the uploaded files to be served after authorization, while the static files remain publicly accessible.
Usage
First you need to activate the middlewares in the settings.yaml
middleware:
# ...
- selva.web.middleware.files.StaticFilesMiddleware
- selva.web.middleware.files.UploadedFilesMiddleware
# ...
After that, files located in the directories resources/static
and resources/uploads
will be served at /static/
and /uploads/
, respectively.
Static files mappings
You can map specific paths to single static files in order to, for example, serve
the favicon at /favicon.ico
pointing to a file in resources/static/
:
middleware:
- selva.web.middleware.files.StaticFilesMiddleware
staticfiles:
mappings:
favicon.ico: my-icon.ico
Configuration options
The available options to configure the StaticFilesMiddleware
and UploadedFilesMiddleware
are shown below:
staticfiles:
path: /static # (1)
root: resources/static # (2)
mappings: {}
uploadedfiles:
path: /uploads # (3)
root: resources/uploads # (4)
- Path where static files are served
- Directory where static files are located
- Path where uploaded files are served
- Directory where uploaded files are located