hyperf访问静态文件

第一步:创建一个中间件

第二部:在中间件中写入代码:

   //静态资源
        $currUrl = trim($request->getUri()->getPath(), '/');
        $extension = pathinfo($currUrl, PATHINFO_EXTENSION);
        if (!empty($extension) && $extension !== 'php') {
//配置文件可看这里:https://www.hyperf.wiki/2.1/#/zh-cn/filesystem?id=%e9%85%8d%e7%bd%ae%e9%9d%99%e6%80%81%e8%b5%84%e6%ba%90
            if (!config('server.settings.enable_static_handler')) {
                throw new NotFoundHttpException();
            }
            //根据url拼接实际的保存路径
            $static_file = config('server.settings.document_root').DIRECTORY_SEPARATOR.'upload' . DIRECTORY_SEPARATOR.'static'.DIRECTORY_SEPARATOR . $currUrl;
            if (!is_file($static_file)) {
                throw new NotFoundHttpException();
            }
            return Context::get(ResponseInterface::class)
                ->withHeader('Server', 'crashing')
                ->withBody(new SwooleFileStream($static_file));
        }
        return $handler->handle($request);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值