路由参数
$app->get('/books/:id', function ($id) {
//Show book identified by $id
});
可选路由参数
$app->
get('user/(:name)', function($name = null) {
echo $name;
});
带有默认值的可选路由参数
$app->get('user/(:name)', function($name = 'John') {
echo $name;
});
用正则表达式限定的路由参数
$app->get('/archive/:year', function ($year) {
echo "You are viewing archives from $year";
})->conditions(array('year' => '(19|20)\d\d'));
定义全局模式
如果希望在全局范围用指定正则表达式限定路由参数,可以使用 pattern
方法:
\Think\Http\Route::setDefaultConditions(array(
'firstName' => '[a-zA-Z]{3,}'
));
你也可以选择覆盖全局限定
$app->get('/hello/:firstName', $callable)
->conditions(array('firstName' => '[a-z]{10,}'));