美文网首页
laravel路由

laravel路由

作者: ISIS卡拉肖克 | 来源:发表于2018-05-14 02:27 被阅读0次

Laravel 常见路由

Route::get();
Route::post();
Route::put();
Route::delete();
Route::options();
Route::match(['get', 'post']); 
Route::any('foo');

路由参数

//单个路由参数
Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});
//多个路由参数
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});
//单个路由参数(可选)
Route::get('user/{id?}', function ($id = 1) {
    return 'User '.$id;
});
//多个路由参数(可选)
Route::get('posts/{post}/comments/{comment?}', function ($postId, $commentId = 1) {
    //
});
//注意:多个参数时,只可以对最后一个参数设置可选,其他位置设置可选会解析错误
 
// 正则约束单个参数
Route::get('user/{name?}', function ($name = 'Jone') {
    return $name;
})->where('name', '\w+');  //约束参数为单词字符(数字、字母、下划线)
 
// 正则约束多个参数
Route::get('user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

相关文章

网友评论

      本文标题:laravel路由

      本文链接:https://www.haomeiwen.com/subject/soordftx.html