【问题标题】:Slim Framework sub routingSlim Framework 子路由
【发布时间】:2016-03-02 13:47:30
【问题描述】:

我想像这样在 Slim Framework v3.2.0 中使用子路由:

  • www.test.com/
  • www.test.com/foodtype/
  • www.test.com/foodtype/page/

据我了解,只能调用一次 get 。目前我的 routes.php 中有这个:

$app->get('/', function () {
// Load index page
});

$app->get('/{foodtype}', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] 
});

如何为 page1 添加单独的可选路由?

我试过了:

$app->get('/{foodtype}/{page}', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] and $args['page']
});

这会导致“找不到页面”错误。我想我也需要转义可选的“/”?

【问题讨论】:

    标签: php routing slim slim-3


    【解决方案1】:

    您必须在原始路线中将页面部分设为可选。

    如:

    $app->get('/{foodtype}', function ($request, $response, $args) {
    // Load page based on the value of $args['foodtype'] 
    });
    

    变成:

    $app->get('/{foodtype}[/{page}]', function ($request, $response, $args) {
    // Load page based on the value of $args['foodtype'] and $args['page']
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 2013-05-31
      相关资源
      最近更新 更多