簡體   English   中英

找不到頁面(Slim Framework)

[英]Page not found (Slim Framework)

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
require '../src/config/db_standings.php';

$app = new \Slim\App;

require '../src/routes/ppg.php';

require '../src/routes/standings.php';

$app->run();

所以這是我的index.php文件,我不明白為什么它只加載最后一條所需的路由。 在這種情況下,只有standings.php 無論我最后添加的路線是什么,它只會加載最后一條路線,而找不到其他頁面。 我究竟做錯了什么?

Standings.php

<?php

Header('Content-Type: application/json; charset=UTF-8');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

$app->get('/api/standings', function(Request $request, Response $response){
    $sql = "SELECT * FROM standings";

    try {
        $db = new db_standings();
        $db = $db->connect();

        $stmt = $db->query($sql);
        $standings = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;

        $json = json_encode($standings, JSON_UNESCAPED_UNICODE);

        echo($json);

    } catch(PDOException $e) {
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

ppg.php

<?php

Header('Content-Type: application/json; charset=UTF-8');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

$app->get('/api/ppg', function(Request $request, Response $response){
    echo 'BANANAS';
});

index.php您正在創建Slim應用程序實例並將其放在$app變量中:

<?php

$app = new \Slim\App; # This is new Slim app contained in $app variable

// executing code of standings.php...
// executing code of ppg.php...

$app->run(); # Here you run the application that is contained in $app variable.

但是,您在每個包含的文件中都執行相同的操作: standings.phpppg.php

因此,當您包含standings.php ,將覆蓋$app變量的內容,並添加文件中聲明的路由。

然后,您包含ppg.php ,它將覆蓋$app實例(以及因此聲明的路由)並追加其路由。

當執行流程返回到index.php ,它將運行$app->run()而該$app恰好是包含在最后一個文件中的那個。

因此,要解決此問題,只需刪除此行

$app = new \Slim\App;

從除index.php所有文件中index.php

更新

在您寫的評論中

刪除不必要的並更改JSON輸出方法后,出現“ Slim Application Error-抱歉給您帶來的不便”

好的,那是因為您做錯了,請原諒我太苛刻了。

本質上,Slim應用程序是一組附加到特定路由的回調。

通常,每個回調都接受兩個參數:請求和響應對象,每個回調(或由於某種原因可能不會)將請求和響應更改為所需的內容。 所有回調均在一個鏈中一個接一個地被調用。

規則很簡單: 任何回調都應在end中返回響應對象 該響應可能是任何內容:html文檔,json數據,文件-可以在HTTP響應中返回的任何內容。

因此,在Slim應用程序中,如果您正在做類似的事情

header('Content-Type: application/json; charset=UTF-8');

要么

echo $jsonString;

那么你做錯了。 而不是設置響應標頭並直接輸出JSON,您應該構建具有該標頭並在body中包含數據的Response對象 Slim在這方面很出色,請看一看(為了更好的抽象,我對您的代碼做了一些簡化):

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

// Append callback to a route: note request response arugments.
$app->get('/api/standings', function(Request $request, Response $response) {

    $standingsRepository = new \StandingsRepository;

    $standings = $standingsRepository->getAll();

    if ($standings) {
    // Return response object
        return $response->withJson($standings);
    } else {
        // Or pass to another callback: note request and response
    return new \Slim\Exception\NotFoundException($request, $response);
    }

});

這里我們使用Response對象的withJson方法。 這會將application/json標頭設置為您的響應,並將您作為參數傳遞的內容放在正文中。 在我們的例子中,它是$ standings變量的值(我假設它是一個對象數組)。

希望我能為您弄清楚一些事情。 我強烈建議您閱讀框架文檔 ,它將花費您一個晚上和兩杯咖啡,並為您節省很多時間。

正如@Georgy Ivanov在以下答案中所建議的那樣,您必須從standings.php和ppg.php中刪除以下塊:

Header('Content-Type: application/json; charset=UTF-8');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

另外,由於您打算輸出JSON響應,因此可以使用Slim的withJson()方法來簡單地對其進行響應:

return $response->withJson($standings);

請參閱此https://www.slimframework.com/docs/objects/response.html#returning-json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM