如何在使用 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 教程中的命令启动我的 slim 应用程序:php -S localhost:8888 -t api index.php
,但是当我尝试导航到 index.html 时收到 404。
我知道我可以在服务于 index.html 的 Slim 中呈现 home
状态,但是是否有另一种方法可以做到这一点而没有我的 API 服务于模板?换句话说,有没有一种方法可以直接导航到我的客户?
api
index.php
public
index.html
目前我正在使用命令 php -S localhost:8888 -t api index.php
来启动我的服务器
目前您将文档根目录设置为 /api
,因此如果不使用包含 html 文件的 php 代码,实际上无法访问 html 文件。因为文件在文档根目录之前(/api
)
在我看来,最好的选择是为此在 slim 中添加一条路由,并在其中包含来自客户端的 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/
访问客户端,使用 domain.com/api/
可以访问 api
注意:您应该仅将 php 服务器用于测试,而不是用于生产。
我开始使用 PHP Slim,我正在创建一个 REST API,我想在它前面放一个客户端。
这是一个简单的问题,但我不知道如何为我的index.html(客户的主页)提供服务。
我正在使用 Slim 教程中的命令启动我的 slim 应用程序:php -S localhost:8888 -t api index.php
,但是当我尝试导航到 index.html 时收到 404。
我知道我可以在服务于 index.html 的 Slim 中呈现 home
状态,但是是否有另一种方法可以做到这一点而没有我的 API 服务于模板?换句话说,有没有一种方法可以直接导航到我的客户?
api
index.php
public
index.html
目前我正在使用命令 php -S localhost:8888 -t api index.php
来启动我的服务器
目前您将文档根目录设置为 /api
,因此如果不使用包含 html 文件的 php 代码,实际上无法访问 html 文件。因为文件在文档根目录之前(/api
)
在我看来,最好的选择是为此在 slim 中添加一条路由,并在其中包含来自客户端的 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/
访问客户端,使用 domain.com/api/
注意:您应该仅将 php 服务器用于测试,而不是用于生产。