修剪外部路由文件错误

时间:2017-04-01 18:59:12

标签: file routes slim

我开始使用Slim(3.8.1)。我正在玩门票样品。

因为我想要一个更大的应用程序,我希望将我的路由放在单独的文件中。但是当我在index.php中包含路由文件时,我收到此错误:

"可捕获的致命错误:传递给Closure :: {closure}()的参数1必须是Request的实例,Slim \ Http \ Request的实例给出"

这是我的路径文件(之前在index.php中的简单副本,包含在php标签中):

<?php
$app->get('/tickets', function (Request $request, Response $response) {
   $this->logger->addInfo("Ticket list");
   $mapper = new TicketMapper($this->db);
   $tickets = $mapper->getTickets();

   $response = $this->view->render($response, "tickets.phtml", ["tickets" => $tickets, "router" => $this->router]);
   return $response;
});
?>

在我的index.php文件中,我现在改为:

require '../routes/tickets.php';

我是否必须以某种方式注册路径文件的路径?

感谢任何帮助。

祝你好运, 乔治

1 个答案:

答案 0 :(得分:0)

我认为以下内容适用于外部路由文件:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app->get('/', function (Request $request, Response $response) 
{
    $response = $this->view->render($response, "home.phtml");
    return $response;
});
?>