[英]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();
So this is my index.php
file and I don't understand why is it behaving the way that it only loads the last required route. 所以这是我的index.php
文件,我不明白为什么它只加载最后一条所需的路由。 In this case only standings.php
. 在这种情况下,只有standings.php
。 Whatever route I add last it only loads the last route and other pages are not found. 无论我最后添加的路线是什么,它只会加载最后一条路线,而找不到其他页面。 What am I doing wrong? 我究竟做错了什么?
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 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';
});
In index.php
you are creating Slim application instance and put it in $app
variable: 在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.
But you do the same thing in each included file: standings.php
and ppg.php
. 但是,您在每个包含的文件中都执行相同的操作: standings.php
和ppg.php
。
So, when you include standings.php
, you overwrite content of the $app
variable, add routes that are declared in the file. 因此,当您包含standings.php
,将覆盖$app
变量的内容,并添加文件中声明的路由。
Then you include ppg.php
that overwrites $app
instance (and, consequently, the routes that had been declared) and appends its routes. 然后,您包含ppg.php
,它将覆盖$app
实例(以及因此声明的路由)并追加其路由。
When the flow of execution returns to index.php
, it runs $app->run()
and that $app
happens to be the one created in the last file included. 当执行流程返回到index.php
,它将运行$app->run()
而该$app
恰好是包含在最后一个文件中的那个。
Thus to fix the problem, simply remove this line 因此,要解决此问题,只需删除此行
$app = new \Slim\App;
from all files but index.php
. 从除index.php
所有文件中index.php
。
UPDATE 更新
In comments you wrote 在您写的评论中
After removing the unnecessary and changing JSON output method I get "Slim Application Error - sorry for the temporary inconvenience" 删除不必要的并更改JSON输出方法后,出现“ Slim Application Error-抱歉给您带来的不便”
Okay, the reason for that is because you're doing it wrong, excuse me for being too harsh. 好的,那是因为您做错了,请原谅我太苛刻了。
In essence, Slim application is a set of callbacks appended to specific routes. 本质上,Slim应用程序是一组附加到特定路由的回调。
Generally each callback accepts two arguments: request and response object, and each migh (or might not for some reason) change request and response to whatever you want. 通常,每个回调都接受两个参数:请求和响应对象,每个回调(或由于某种原因可能不会)将请求和响应更改为所需的内容。 All callbacks are invoked one after another, in a chain. 所有回调均在一个链中一个接一个地被调用。
The rule is simple: any callback should return response object in the end . 规则很简单: 任何回调都应在end中返回响应对象 。 And that response may be anything: html document, json data, a file - whatever can be returned in HTTP response. 该响应可能是任何内容:html文档,json数据,文件-可以在HTTP响应中返回的任何内容。
Therefore in your Slim application if you're doning something like 因此,在Slim应用程序中,如果您正在做类似的事情
header('Content-Type: application/json; charset=UTF-8');
or 要么
echo $jsonString;
then you're doing it wrong. 那么你做错了。 Instead of setting response header and outputting JSON directly, you should build Response object that has that header and contains your data in body . 而不是设置响应标头并直接输出JSON,您应该构建具有该标头并在body中包含数据的Response对象 。 Slim is excellent at that, take a look (I've simplified your code a bit for better abstraction): 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);
}
});
Here we used withJson
method of Response
object. 这里我们使用Response
对象的withJson
方法。 This sets application/json
header to your response and puts in body whatever you pass as argument. 这会将application/json
标头设置为您的响应,并将您作为参数传递的内容放在正文中。 In our case it's value of $standings variable (I assume it's an array of objects). 在我们的例子中,它是$ standings变量的值(我假设它是一个对象数组)。
I hope I made things a bit more clear for you. 希望我能为您弄清楚一些事情。 I strongly suggest to read framework documentation , it will take you one evening and two cups of coffee and save you so much time. 我强烈建议您阅读框架文档 ,它将花费您一个晚上和两杯咖啡,并为您节省很多时间。
As suggested by @Georgy Ivanov in the below answer you have to remove following block from standings.php and ppg.php: 正如@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;
In addition, since your intention is to output a JSON response you can simply to that by using Slim's withJson() method: 另外,由于您打算输出JSON响应,因此可以使用Slim的withJson()方法来简单地对其进行响应:
return $response->withJson($standings);
refer to this https://www.slimframework.com/docs/objects/response.html#returning-json 请参阅此https://www.slimframework.com/docs/objects/response.html#returning-json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.