【问题标题】:slim optional route parameter suffix not working苗条的可选路由参数后缀不起作用
【发布时间】:2015-05-01 06:33:06
【问题描述】:

我想定义一个最后有一个 .suffix 参数的路由。我想在我的应用程序中使用它来返回用户需要的东西。例如 .json 或 .xml 或没有!。我应该如何定义我的路线?这是我想要的示例地址:

/user/all.json

/user/all.xml

/user/all # default json

这是我定义的路线。但它没有按预期工作。

/user/:method(\.(:type))

【问题讨论】:

标签: slim


【解决方案1】:

在 Slim 中,路线段由正斜杠定义,不能与点互换。但是,您可以向路由闭包添加逻辑,也可以向多个路由添加路由条件。

在闭包中:

$app->get('/user/:methodtype', function ($methodtype) {
    if ($path = explode($methodtype, '.')) {
        if ($path[1] === 'json') {
            // Something to do with json...
        } elseif ($path[1] === 'xml') {
            // Something to do with xml...
        }
    }
});

或者使用路由条件,每个条件一个,所以它们是互斥的:

// JSON requests
$app->get('/user/:methodtype', function ($methodtype) {
    // Something to do with json...
})->conditions(array('methodtype' => '.json$'));

// XML requests
$app->get('/user/:methodtype', function ($methodtype) {
    // Something to do with xml...
})->conditions(array('methodtype' => '.xml$'));

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 2016-01-26
    • 2018-01-08
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2019-09-15
    • 2017-05-08
    相关资源
    最近更新 更多