laravel路由定义

参考http://www.ruchee.com/notes/fms/laravel_primer.html

 

路由

路由定义位置在 app/routes.php 文件,支持五种路由方法,采用回调函数的形式工作:

Route::get()
Route::post()
Route::put()
Route::delete()
Route::any()

 

相当重要的一点:路由是区分大小写的,/hello 和 /Hello 是不同的路由

可以用 {} 对路由进行命名,如:

Route::get('/hello/{name}', function ($name) {
    return "Hello, {$name}";
});

 

还可以在命名路由后加一个 ? 号,表示该路由可有可无,如:

Route::get('/hello/{name?}', function ($name) {
    return "Hello, {$name}";
});
// 但需要注意的是,如果 $name 没有赋默认值,则访问 /hello 路径会出错

可以通过 
View::make 使用模版,如:
Route::get('/hello/{name?}', function ($name = 'Laravel') {
    $data['name'] = $name;
    return View::make('hello', $data);
});
// 上述代码会用 $data 数组去填充 app/views 目录下的 hello.php 文件

 

其他用法

跳转:Redirect::to('需要跳转到的路由')
自定义响应:Response::make('响应内容', HTTP状态码)

 

posted @ 2015-02-02 19:57  自由出土文物  阅读(1556)  评论(0编辑  收藏  举报