找不到页面(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 实例(以及因此,已声明的路由)并附加其路由。

当执行流程 return 到 index.php 时,它运行 $app->run() 并且 $app 恰好是在包含的最后一个文件中创建的。

因此要解决问题,只需删除此行

$app = new \Slim\App;

来自除 index.php.

之外的所有文件

更新

在您写的评论中

After removing the unnecessary and changing JSON output method I get "Slim Application Error - sorry for the temporary inconvenience"

好吧,那是因为你做错了,原谅我太苛刻了。

从本质上讲,Slim 应用程序是一组附加到特定路由的回调。

通常每个回调接受两个参数:请求和响应 object,每个回调都可能(或由于某种原因可能不会)将请求和响应更改为您想要的任何内容。所有的回调一个接一个地被调用,在一个链中。

规则很简单:任何回调最后都应该return响应object。该响应可以是任何内容:html 文档、json 数据、文件 - 可以在 HTTP 响应中 return 编辑的任何内容。

因此在您的 Slim 应用程序中,如果您正在做类似

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

echo $jsonString;

那你就错了。与其设置响应 header 并直接输出 JSON,您应该 构建具有 header 并在 body 中包含数据的响应 object 。 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);
    }

});

这里我们使用了Responseobject的withJson方法。这会将 application/json header 设置为您的响应,并将您作为参数传递的任何内容放入 body 中。在我们的例子中,它是 $standings 变量的值(我假设它是一个 objects 的数组)。

我希望我让你更清楚一些。我强烈建议阅读framework documentation,它会花费你一个晚上和两杯咖啡,节省你很多时间。

正如@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