slim可选路由参数后缀不起作用

时间:2015-04-23 10:04:15

标签: slim

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

            <div ng-show="template.type === 'building'" class="vertical-spacing">
            <label>Ressource gain per level</label>

            <ul class="list-group">
                <li ng-repeat="level in template.ressource.level track by $id(level)" 
                    class="list-group-item" 
                    ng-hide="template.ressource.level.indexOf(level) === 0">
                    <span>
                        Level: {{ template.ressource.level.indexOf(level) }}
                    </span>

                    <span class="pull-right">
                        <a href="" ng-click="removeLevel(level)">
                            <span class="glyphicon glyphicon-remove"></span>
                        </a>
                    </span>

                    <ul>
                        <li class="list-group-item" ng-repeat="gain in level.gain track by $id(gain)">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>Amount</label>
                                    <input type="number" 
                                           class="form-control"
                                           ng-model="gain.amount" 
                                           min="1" 
                                           required>
                                </div>

                                <div class="form-group">
                                    <label>Ressource</label>
                                    <select required 
                                            ng-model="gain.ressource" 
                                            ng-init="gain.ressource = gain.ressource || ressources.basic[0]"
                                            ng-options="ressource._id as ressource.name | capitalize for ressource in ressources.basic"
                                            class="form-control selectWidth">
                                        <option style="display:none" value="" disabled>select a ressource</option>
                                    </select>
                                </div>

                                <span class="pull-right">
                                    <a href="" ng-click="removeGain(level, gain)">
                                        <span class="glyphicon glyphicon-remove"></span>
                                    </a>
                                </span>
                            </div>
                        </li>
                    </ul>

                    <span>
                        <a href="" ng-click="addGain(level)" ng-hide="(level.gain.length + 1) > ressources.basic.length">
                            <span class="glyphicon glyphicon-plus"></span> Add ressource
                        </a>
                    </span>
                </li>
            </ul>

            <span>
                <a href="" ng-click="addLevel()">
                    <span class="glyphicon glyphicon-plus"></span> Add level
                </a>
            </span>
        </div>

        <div ng-show="template.type === 'building'" class="vertical-spacing">
            <label>Ressource cost per level</label>

            <ul class="list-group">
                <li ng-repeat="level in template.ressource.level track by $id(level)" 
                    class="list-group-item" 
                    ng-hide="template.ressource.level.indexOf(level) === 0">
                    <span>
                        Level: {{ template.ressource.level.indexOf(level) }}
                    </span>

                    <span class="pull-right">
                        <a href="" ng-click="removeLevel(level)">
                            <span class="glyphicon glyphicon-remove"></span>
                        </a>
                    </span>

                    <ul>
                        <li class="list-group-item" ng-repeat="cost in level.cost track by $id(cost)">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>Amount</label>
                                    <input type="number" 
                                           class="form-control"
                                           ng-model="cost.amount" 
                                           min="1" 
                                           required>
                                </div>

                                <div class="form-group">
                                    <label>Ressource</label>
                                    <select required 
                                            ng-model="cost.ressource" 
                                            ng-init="cost.ressource = cost.ressource || ressources.categories[0]"
                                            ng-options="ressource._id as ressource.name | capitalize for ressource in ressources.categories"
                                            class="form-control selectWidth">
                                        <option style="display:none" value="" disabled>select a ressource</option>
                                    </select>
                                </div>

                                <span class="pull-right">
                                    <a href="" ng-click="removeCost(level, cost)">
                                        <span class="glyphicon glyphicon-remove"></span>
                                    </a>
                                </span>
                            </div>
                        </li>
                    </ul>

                    <span>
                        <a href="" ng-click="addCost(level)" ng-hide="(level.cost.length + 1) > ressources.categories.length">
                            <span class="glyphicon glyphicon-plus"></span> Add ressource
                        </a>
                    </span>
                </li>
            </ul>

            <span>
                <a href="" ng-click="addLevel()">
                    <span class="glyphicon glyphicon-plus"></span> Add level
                </a>
            </span>
        </div>

/user/all.json

/user/all.xml

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

/user/all # default json

1 个答案:

答案 0 :(得分: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$'));