
Laravel 基础
路由
通过 Route 类和 HTTP 请求对应的方法,在以下文件中,绑定 URI 和匿名函数,或控制器名@方法名,组成路由:
匿名函数
Response 内容由 return 返回:
# 定义 HTTP get 可访问路由
Route::get('/hello', function () { # Route::get 为门面模式 (Facade Pattern) 非静态访问
return 'Hello, Moha Online!';
});
控制器名@方法名
# 定义任意方法皆可访问的路由
Route::any('login', 'MohaController@login');
路由参数
绑定的 URI 声明时中可用 {}
嵌入参数,嵌入顺序与绑定函数参数顺序匹配:
# 带参数路由
Route::put('post/{id}/edit', function ($id) {
});
路由参数顺序需与函数/方法参数顺序一致。不定参数(按需传入参数)用问号标示,相应形参需有默认值。
# 不定参数路由
Route::match(['get', 'post'], 'post/{id}/comments/{$id?}', function ($post_id, $comment_id = 0) {
});