fastadmin接口输出图片 自动拼接网站URL

先自定义常量

复制代码
1.文件接口路径 修改核心文件  application\common\controller\Api.php
    /**
     * 构造方法
     * @access public
     * @param Request $request Request 对象
     */
    public function __construct(Request $request = null)
    {
        $this->request = is_null($request) ? Request::instance() : $request;

        // 控制器初始化
        $this->_initialize();
       //<--代码位置 -->
        if (!defined('IMG_PRE')) {
            define("IMG_PRE", $this->request->domain());//定义获取域名常量
//            define("IMG_PRE", 'https://yunlingshidai.oss-cn-shanghai.aliyuncs.com');//也可以是云端域名
        }
        //<--代码位置 -->
        // 前置操作方法
        if ($this->beforeActionList) {
            foreach ($this->beforeActionList as $method => $options) {
                is_numeric($method) ?
                    $this->beforeAction($options) :
                    $this->beforeAction($method, $options);
            }
        }
    }

ffastadmin 接口输出图片

复制代码
    //修改器
    public function getImageAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $domain=defined("IMG_PRE")?IMG_PRE:'';
        $preg = "/^http(s)?:\\/\\/.+/";
        if(preg_match($preg,$value))
        {
            $data=$value;
        }else{
            $data=$domain.$value;
        }
        return $data;
    }
相关推荐
开源技术6 小时前
Fastapi教程: 身份认证与授权03——带有 password 和 Bearer 的简单 OAuth2
fastapi
java1234_小锋1 天前
FastAPI python web开发- 表单数据与模型 & 请求表单与文件 & 响应类型 - 返回文件类型
fastapi
hboot2 天前
AI工程师第五课 - 大语言模型基础
python·llm·fastapi
喜欢的名字被抢了2 天前
Python实战:SQLAlchemy ORM与FastAPI项目集成
开发语言·python·sql·教程·fastapi
阿豪只会阿巴2 天前
两小时快速入门 FastAPI--第二回
windows·python·fastapi
阿豪只会阿巴2 天前
两小时快速入门 FastAPI--第一回
开发语言·python·fastapi
迷途呀3 天前
新闻头条后端:新闻缓存模块
前端·redis·python·缓存·fastapi
CaffeinePro4 天前
FastAPI数据库集成SQLAlchemy异步ORM全方案
后端·fastapi
能有时光4 天前
从卡顿到丝滑:FastAPI 调用外部 API 的正确姿势(httpx 实战)
fastapi·httpx
Mr.朱鹏4 天前
【FastAPI 全栈实战 | 第2篇】Pydantic 数据校验与响应模型 —— 让 Bug 死在请求进来的那一刻
人工智能·python·fastapi