如何使用slim Framework分离不同目录中的不同路由

I am having some issues setting up routes in my app in different directories. The app only loads one of the routes files.

File Structure

 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';
});

So with the above structure, only one on the routes file is loaded depending on the order in the file structure. Either the book routes or the customer routes and the order will return a Page not Found Error.

I don't understand.T

查看全部
dongxian7471
dongxian7471
2019/02/16 13:51
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复