php - Slim 代理重定向到 VueJS localhost 服务器
问题描述
我正在使用 PHP SLIM框架,并且我想为vuejs dev
localhost(服务器端)上的服务器提供开发服务。
我想做这样的事情:
$app->get('/dev/vuejs', function (Request $request, Response $response, array $args) {
return $response->withRedirect("http://127.0.0.1:8080");
});
这不像我想的那样工作,实际上只是告诉客户端寻找那个 URI。
解决方案
你的问题很难理解,但我猜你试图*.vue
通过 Slim 在vuejs/vue-dev-server
.
这是使用 curl 的概念证明:
$app->get('/dev/vuejs', function (Request $request, Response $response, array $args) {
// Change the url here
$url = 'http://127.0.0.1:8080';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$responseBody = curl_exec($ch);
if ($responseBody === false) {
$response->getBody()->write('HTTP error: ' . curl_error($ch));
return $response->withStatus(500);
}
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = trim(substr($responseBody, $headerSize));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response->getBody()->write($responseBody);
return $response->withStatus($httpCode);
});
推荐阅读
- javascript - 使用 javascript 删除包含标签和文本框的动态添加的表单组
- html - Bootstrap 4 .card 溢出时受父高度限制
- r - 分组并保留具有匹配模式的列
- flutter - 如何使用 Flutter 和 Dart 在后台监听 BLE 信号?
- c++ - 当从仿函数类模板参数推导出(衰减)类型时,完美转发失败。为什么?
- scala - Flink Streaming - 每天触发的 10 天事件时间窗口
- c++ - 使用 new[] 关键字时不允许不完整类型
- python - Python逐行读取并保持缩进
- automation - 在 sikulix 中,我如何制作一个 while 循环,条件是它在倒计时结束后不会重复?
- python - 如何使用 python 解析 xsd 并从中创建 XML?