首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >瘦框架^3多个路径文件

瘦框架^3多个路径文件
EN

Stack Overflow用户
提问于 2018-06-06 14:59:11
回答 1查看 1.9K关注 0票数 2

我正在尝试使用瘦框架创建一个API。在我在youtube上看的一篇教程中,他将路由放在一个单独的文件中,这似乎是一个很好的主意,直到我添加了一个额外的文件,即两个单独的带有路由的php文件。然后默认为最后一个包含的文件,从不查看第一个文件。我已经尝试了每一个可能的组合来使这个工作,包括创建一个组和包括在组中的文件。在一个更大的应用程序中,如果不能更好地组织index.php文件,它将变得非常丑陋。

也许我错过了什么,但对我来说似乎很简单。LOL

index.php

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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;
});

如果在另一个没有回答的问题上回答了这个问题,请自由地把我转向那个方向。但我看到的一切都比不上我看到的问题。

阿米戈斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 15:02:41

您正在每个文件中创建一个新的app实例,您应该只有一个

代码语言:javascript
复制
$app = new \Slim\App;

然后,您可以在不同的文件中添加到这个应用程序实例的路由。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50723554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档