如何使用 slim Framework 分隔不同目录中的不同路由
How to separate different routes in different directory with slim Framwork
我在我的应用程序的不同目录中设置路由时遇到了一些问题。该应用程序仅加载其中一个路由文件。
文件结构
myapp
|_public
| |_vendor
| |_index.php
|_src
|_routes
|_books.php
|_customers.php
index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require './vendor/autoload.php';
$app = new \Slim\App;
// Routes
require '../src/routes/books.php';
require '../src/routes/customers.php';
$app->run();
customers.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/customers', function(Request $request, Response $response){
echo 'Customers';
});
books.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/books', function(Request $request, Response $response){
echo 'Books';
});
所以对于上面的结构,根据文件结构中的顺序,只加载路由文件中的一个。预订路线或客户路线和订单将 return 找不到页面错误。
我没有understand.T
您正在每个路由文件中创建一个新的 $app
实例,它会覆盖 index.php
文件中的实例。
尝试从您的路由文件中删除 $app
个实例以解决问题:
customers.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// $app = new \Slim\App; <-- remove this line
$app->get('/api/customers', function(Request $request, Response $response){
echo 'Customers';
});
books.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// $app = new \Slim\App; <-- remove this line
$app->get('/api/books', function(Request $request, Response $response){
echo 'Books';
});
我在我的应用程序的不同目录中设置路由时遇到了一些问题。该应用程序仅加载其中一个路由文件。
文件结构
myapp
|_public
| |_vendor
| |_index.php
|_src
|_routes
|_books.php
|_customers.php
index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require './vendor/autoload.php';
$app = new \Slim\App;
// Routes
require '../src/routes/books.php';
require '../src/routes/customers.php';
$app->run();
customers.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/customers', function(Request $request, Response $response){
echo 'Customers';
});
books.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/books', function(Request $request, Response $response){
echo 'Books';
});
所以对于上面的结构,根据文件结构中的顺序,只加载路由文件中的一个。预订路线或客户路线和订单将 return 找不到页面错误。
我没有understand.T
您正在每个路由文件中创建一个新的 $app
实例,它会覆盖 index.php
文件中的实例。
尝试从您的路由文件中删除 $app
个实例以解决问题:
customers.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// $app = new \Slim\App; <-- remove this line
$app->get('/api/customers', function(Request $request, Response $response){
echo 'Customers';
});
books.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// $app = new \Slim\App; <-- remove this line
$app->get('/api/books', function(Request $request, Response $response){
echo 'Books';
});