繁体   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