我的烦恼你来解决 2025-07-09 15:42 采纳率: 71.4%
浏览 23

如何使用swoole请求thinkphp5-fastadmin框架在首次登录之后的模版页面的部分代码?

使用swoole请求thinkphp5-fastadmin框架
在首次登录之后的模版页面的部分代码


<?php if (!defined('THINK_PATH')) exit(); /*a:10:{s:69:"/var/www/html/server/../application/common/view/tpl/dispatch_jump.tpl";i:1736236336;s:66:"/var/www/html/server/../application/firmiana/view/index/login.html";i:1736236337;s:70:"/var/www/html/server/../application/firmiana/view/dashboard/index.html";i:1741931344;s:66:"/var/www/html/server/../application/firmiana/view/index/index.html";i:1736236337;s:59:"/var/www/html/application/firmiana/view/layout/default.html";"/var/www/html/application/firmiana/view/common/meta.html";

刷新之后的模版部分代码

<?php if (!defined('THINK_PATH')) exit(); /*a:6:{s:66:"/var/www/html/server/../application/firmiana/view/index/index.html";i:1736236337;s:56:"/var/www/html/application/firmiana/view/common/meta.html";


为什么会首次登录使用编译缓存和刷新之后使用的编译缓存不一致,或者为什么没有生成一样的编译缓存文件


 public function __construct()
    {
        $this->serv = new \Swoole\Http\Server($this->host, $this->port);

        $this->serv->set(array(
            'worker_num' => 1,
            'max_request' => 1000,
            'daemonize' => 0,
            'document_root' => __DIR__ . "/../public",
            'debug_mode' => 1,
            'buffer_output_size'=>32 * 1024 * 1024,
            'socket_buffer_size' => 32 * 1024 * 1024,
            "enable_static_handler" => true,
        ));

        //热更新
        $this->startFileWatcher();

        $this->serv->on('WorkerStart', function ($server, $worker_id) {
            // 定义应用路径
            define('APP_PATH', __DIR__ . '/../application/');
            // 加载thinkphp5.0 框架的基础文件
            require __DIR__ . '/../thinkphp/base.php';
        });

        // 监听服务器启动事件
        $this->serv->on('start', function ($server) {
            echo '服务器已启动,监听端口:' . $this->port . PHP_EOL;
        });


        $this->serv->on('request', function ($request, $response) {
            go(function () use ($request, $response){
                Log::write('Http server:'.json_encode($request->server),'App');
                // 处理静态文件请求
                $uri = $request->server['request_uri'];
               if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg)$/', $uri)) {
                   $file = __DIR__ . '/../public' . $uri;
                   if (file_exists($file)) {

                       if(!is_readable($file)){
                           Log::write('File is not readable: ' . $file, 'App');
                           $response->status(403);
                           $response->end('Forbidden');
                           return;
                       }
                       $response->header('Content-Type', $this->getMimeType($file));
                       // 尝试使用sendfile
                       if ($response->sendfile($file) === false) {
                           // 如果失败,改用分块读取发送
                           $fp = fopen($file, 'r');
                           if ($fp) {
                               while (!feof($fp)) {
                                   $chunk = fread($fp, 8192);
                                   $response->write($chunk);
                               }
                               fclose($fp);
                           }
                           $response->end();
                       }
                       return;
                   } else {
                       Log::write('File exists: ' . $file, 'App');
                       $response->status(404);
                       $response->end('File not found');
                       return;
                   }
               }

               // 处理 favicon.ico
               if ($uri == '/favicon.ico') {
                   $response->status(404);
                   $response->end();
                   return;
               }
//                // 处理根路径重定向
//                if ($uri === '/') {
//                    $response->redirect('/index/login', 302);
//                    return;
//                }

                // 初始化全局变量
                $_GET = ['s'=> parse_url($uri, PHP_URL_PATH)];
                if (isset($request->get)) {
                    foreach ($request->get as $k => $v) {
                        $_GET[$k] = $v;
                    }
                }
                $_POST = [];
                if (isset($request->post)) {
                    foreach ($request->post as $k => $v) {
                        $_POST[$k] = $v;
                    }
                }
                // 初始化文件上传变量
                $_FILES = [];
                if (isset($request->files)) {
                    foreach ($request->files as $k => $v) {
                        $_FILES[$k] = $v;
                    }
                }
                $_SERVER = [];
                $host = $request->header['host'];
                // 如果 Host 头不包含端口,手动添加
                if (strpos($host, ':') === false) {
                    $host .= ":$this->port"; // 或者动态获取 $this->port
                }
                if (isset($request->header)) {
                    foreach ($request->header as $k => $v) {
                        $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $k))] = $v;
                        if($k=='host'){
                            $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $k))]=$host;
                        }
                    }
                }
                if(isset($request->server)){
                    foreach ($request->server as $k => $v) {
                        $_SERVER[strtoupper($k)] = $v;
                    }
                }
                $_SERVER['SCRIPT_NAME'] = '/index.php';   // 保持框架路由标识
                $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true); // Add this line
                Log::write('Http server:'.json_encode($_SERVER),'App');
                $_COOKIE = [];
                if (isset($request->cookie)) {
                    foreach ($request->cookie as $k => $v) {
                        $_COOKIE[$k] = $v;
                    }
                }

                $param=array_merge($_GET,$_POST);
                // 重新初始化 ThinkPHP 的请求对象 (关键步骤) 要不然POST请求都无法识别
                 Request::create($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'],$param, $_COOKIE,$_FILES, $_SERVER);
                // 运行应用
                ob_start();
                try {
//                    $thinkResponse = App::run();
                    App::run()->send();
                } catch (\Exception $e) {
                    $response->status(500);
                    $response->end('Internal Server Error: ' . $e->getMessage());
                    return;
                }
                $result = ob_get_clean();
//                // 处理响应
//                $response->status($thinkResponse->getCode());
//                // 根据内容类型设置响应头
//                foreach ($thinkResponse->getHeader() as $name => $value) {
//                    $response->header($name, $value);
//                }
//                $response->end($thinkResponse->getContent());
                $response->end($result);
            });
        });

        // 启动服务
        $this->serv->start();
    }

怎么解决

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-07-09 15:42
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解决方案

    使用 Swoole 请求 ThinkPHP5-Fastadmin 框架的模板页面,可以使用 Swoole 的 Coroutine 功能来实现。下面是一个示例代码:

    <?php
    use Swoole\Coroutine;
    
    // 创建协程
    Coroutine::create(function () {
        // 设置请求头
        $headers = array(
            'Content-Type' => 'application/x-www-form-urlencoded',
        );
        // 设置请求参数
        $params = array(
            'username' => 'your_username',
            'password' => 'your_password',
        );
        // 发送请求
        $response = Swoole\http\Request::post('http://your_fastadmin_url/index.php?s=/Home/Login/login', $params, $headers);
        // 获取响应内容
        $content = $response->body;
        // 解析响应内容
        $data = json_decode($content, true);
        // 处理响应数据
        if ($data['code'] == 200) {
            // 登录成功,获取模板页面内容
            $template_content = Swoole\http\Request::get('http://your_fastadmin_url/index.php?s=/Home/Index/index');
            // 处理模板页面内容
            // ...
        } else {
            // 登录失败,处理错误信息
            // ...
        }
    });
    

    在上面的代码中,我们使用 Swoole 的 Coroutine 功能创建了一个协程,然后使用 Swoole\http\Request 类发送请求到 Fastadmin 的登录接口,并获取响应内容。最后,我们使用 json_decode 函数解析响应内容,并处理响应数据。

    需要注意的是,在上面的代码中,我们使用 Swoole\http\Request 类发送请求,这需要在 Swoole 服务器上启用 http 模块。同时,我们也需要在 Fastadmin 的配置文件中启用 http 模块。

    如果你需要获取模板页面的部分代码,可以使用 Swoole\http\Request 类发送请求到 Fastadmin 的模板页面接口,并获取响应内容。例如:

    $template_content = Swoole\http\Request::get('http://your_fastadmin_url/index.php?s=/Home/Index/index');
    

    在上面的代码中,我们使用 Swoole\http\Request 类发送 GET 请求到 Fastadmin 的模板页面接口,并获取响应内容。然后,我们可以使用 json_decode 函数解析响应内容,并处理响应数据。

    评论

报告相同问题?

问题事件

  • 修改了问题 7月9日
  • 创建了问题 7月9日