繁体   English   中英

如何在使用PHP Slim REST Api时提供index.html文件?

[英]How do I serve my index.html file while using PHP Slim REST Api?

我开始使用PHP Slim,我正在创建一个REST API,我想把一个客户端放在前面。

这是一个简单的问题,但我无法弄清楚如何服务我的index.html(客户端的主页)。

我正在使用Slim教程中的命令启动我的瘦身应用程序: php -S localhost:8888 -t api index.php ,但是当我尝试导航到index.html时,我得到了404。

我知道我可以在Slim中呈现一个为index.html提供服务的home状态,但还有另一种方法可以让我的API没有服务模板吗? 换句话说,有没有办法可以直接导航到我的客户端?

api
  index.php
public
  index.html

目前我正在使用命令, php -S localhost:8888 -t api index.php来启动我的服务器

目前你将文档根设置为/api所以实际上没有办法访问html文件而不使用PHP代码来包含html文件。 因为文件在文档根目录( /api )之前

在我看来,最好的选择是添加一个苗条的路线,并在其中包含来自客户端的index.html,然后显示它

$app->get('/clientindex', function ($request, $response, $args) {
    $file = '../public/index.html';
    if (file_exists($file)) {
        return $response->write(file_get_contents($file));
    } else {
        throw new \Slim\Exception\NotFoundException($request, $response);
    }
})

你也可以这样做:

/api
    /index.php // do slim stuff
index.html // display client

当你然后启动PHP服务器没有文件和路径php -S localhost:8888

客户端可以与访问domain.com/并使用该API domain.com/api/

注意:您应该仅将php服务器用于测试而不是生产中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM