簡體   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