【问题标题】:Slim external route file errorSlim 外部路由文件错误
【发布时间】:2017-04-02 03:27:37
【问题描述】:

我开始使用 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';

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

感谢任何帮助。

最好的问候, 乔治

【问题讨论】:

    标签: file routes slim


    【解决方案1】:

    我认为在外部路由文件中可以使用以下方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多