【问题标题】:Slim Framework ^3 multiple route filesSlim Framework ^3 多个路由文件
【发布时间】:2018-06-06 14:59:11
【问题描述】:

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

也许我遗漏了一些东西,但对我来说似乎很简单。哈哈

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

如果在另一个错过的问题中回答了这个问题,请随时将我转向那个方向。但我所看到的一切都与我所看到的问题不符。

亲爱的朋友

【问题讨论】:

    标签: php routing slim


    【解决方案1】:

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

    $app = new \Slim\App;
    

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

    【讨论】:

    • 我试过了,但没有用。. 遇到关于找不到路线的同样微小的错误。
    • 你只在 index.php 中创建值吗?
    • 好的,我收回它。我已经删除了请求和响应行以及 $app 行,但这不起作用。我只是尝试删除 $app 行,这似乎解决了我的问题。谢谢兄弟。我会尽快将其作为答案..
    • 您还可以在 $app-&gt;group(...) 中要求您的路线。
    • 对不起,我没有得到这个在哪里删除 $app 行?在索引文件中还是在路由文件中?
    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多