我正在尝试使用瘦框架创建一个API。在我在youtube上看的一篇教程中,他将路由放在一个单独的文件中,这似乎是一个很好的主意,直到我添加了一个额外的文件,即两个单独的带有路由的php文件。然后默认为最后一个包含的文件,从不查看第一个文件。我已经尝试了每一个可能的组合来使这个工作,包括创建一个组和包括在组中的文件。在一个更大的应用程序中,如果不能更好地组织index.php文件,它将变得非常丑陋。
也许我错过了什么,但对我来说似乎很简单。LOL
index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
//labor routes
require '../src/routes/labor.php';
// Testing routes
require '../src/routes/testing.php';
$app->run();labor.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// GET all customers
// Default Route
$app->get('/api/labor', function(Request $request, Response $response){
$response->getBody()->write("Hello, This is the Celltron, Inc. API for
internal web. Your IP address has been logged and notification sent to the
Administrator.");
return $response;
});testing.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// GET all testing
// Default Route
$app->get('/api/testing', function(Request $request, Response $response){
$response->getBody()->write("Hello, This is the Celltron, Inc. testing API
for internal web. Your IP address has been logged and notification sent to
the Administrator.");
return $response;
});如果在另一个没有回答的问题上回答了这个问题,请自由地把我转向那个方向。但我看到的一切都比不上我看到的问题。
阿米戈斯
发布于 2018-06-06 15:02:41
您正在每个文件中创建一个新的app实例,您应该只有一个
$app = new \Slim\App;然后,您可以在不同的文件中添加到这个应用程序实例的路由。
https://stackoverflow.com/questions/50723554
复制相似问题