<?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();
Run Code Online (Sandbox Code Playgroud)
所以这是我的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().'}';
}
});
Run Code Online (Sandbox Code Playgroud)
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';
});
Run Code Online (Sandbox Code Playgroud)
在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.
Run Code Online (Sandbox Code Playgroud)
但是你在每个包含的文件中做同样的事情:standings.php和ppg.php.
因此,当您包含时standings.php,覆盖$app变量的内容,添加在文件中声明的路由.
然后包括ppg.php覆盖$app实例(以及因此已声明的路由)并附加其路由.
当执行流程返回时index.php,它会运行$app->run(),并且$app恰好是在包含的最后一个文件中创建的流程.
因此,要解决此问题,只需删除此行
$app = new \Slim\App;
Run Code Online (Sandbox Code Playgroud)
从所有文件但index.php.
UPDATE
在你写的评论中
删除不必要的和更改JSON输出方法后,我得到"超薄应用程序错误 - 抱歉暂时不便"
好吧,原因是因为你做错了,请原谅我过于苛刻.
实质上,Slim应用程序是一组附加到特定路由的回调.
通常,每个回调都接受两个参数:请求和响应对象,每个migh(或可能不是出于某种原因)将请求和响应更改为您想要的任何内容.所有回调都是在链中一个接一个地调用.
规则很简单:任何回调都应该在最后返回响应对象.并且该响应可以是任何内容:html文档,json数据,文件 - 在HTTP响应中可以返回任何内容.
因此,在你的Slim应用程序中,如果你喜欢这样的东西
header('Content-Type: application/json; charset=UTF-8');
Run Code Online (Sandbox Code Playgroud)
要么
echo $jsonString;
Run Code Online (Sandbox Code Playgroud)
那你做错了.您应该构建具有该标头并在正文中包含数据的Response对象,而不是直接设置响应标头和输出JSON .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);
}
});
Run Code Online (Sandbox Code Playgroud)
这里我们使用withJson了Response对象的方法.这application/json会将标题设置为您的响应,并将您传递的内容放入正文中作为参数.在我们的例子中,它是$ standings变量的值(我假设它是一个对象数组).
我希望我能为你做一些事情.我强烈建议您阅读框架文档,它将带您一个晚上和两杯咖啡,并节省您这么多时间.
| 归档时间: |
|
| 查看次数: |
4090 次 |
| 最近记录: |