首页 > 解决方案 > Slim 代理重定向到 VueJS localhost 服务器

问题描述

我正在使用 PHP SLIM框架,并且我想为vuejs devlocalhost(服务器端)上的服务器提供开发服务。

我想做这样的事情:

$app->get('/dev/vuejs', function (Request $request, Response $response, array $args) {
    return $response->withRedirect("http://127.0.0.1:8080");
});

这不像我想的那样工作,实际上只是告诉客户端寻找那个 URI。

我想要的是 : 在此处输入图像描述

我得到了什么: 在此处输入图像描述

标签: phpapachevue.jsslim

解决方案


你的问题很难理解,但我猜你试图*.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);
});

推荐阅读