Handling Static Files in Webman

Webman supports access to static files, which are placed in the public directory. For example, accessing http://127.0.0.8787/upload/avatar.png actually corresponds to {main project directory}/public/upload/avatar.png.

Note
Static file access starting with /app/xx/filename actually accesses the public directory of the application plugin. This means that access to directories under {main project directory}/public/app/ is not supported.
For more information, please refer to Application Plugins

Disabling Static File Support

If you do not need static file support, open config/static.php and change the enable option to false. Once disabled, all static file access will return a 404.

Changing the Static File Directory

Webman uses the public directory as the static file directory by default. If you wish to modify this, change the public_path() helper function in support/helpers.php.

Static File Middleware

Webman comes with a static file middleware located at app/middleware/StaticFile.php. Sometimes, we need to perform some processing on static files, such as adding cross-origin HTTP headers or prohibiting access to files that begin with a dot (.). This middleware can be used for such purposes.

The content of app/middleware/StaticFile.php is similar to the following:

<?php
namespace support\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class StaticFile implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        // Prohibit access to hidden files starting with a dot
        if (strpos($request->path(), '/.') !== false) {
            return response('<h1>403 forbidden</h1>', 403);
        }
        /** @var Response $response */
        $response = $next($request);
        // Add cross-origin HTTP headers
        /*$response->withHeaders([
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Credentials' => 'true',
        ]);*/
        return $response;
    }
}

If you need this middleware, you must enable it in the middleware option in config/static.php.