laravel路由使用【总结】

一、路由参数laravel

  • 必选参数

  有时咱们须要在路由中捕获 URI 片断。好比,要从 URL 中捕获用户 ID,须要经过以下方式定义路由参数:spa

1 Route::get('/test_param/{id}', 'TestSomethingController@testParam');
1 class TestSomethingController extends Controller
2 {
3     //
4     public function testParam($id)
5     {
6         echo $id;
7     }
8 }

这个id能够直接经过参数的形式在controller的方法中直接使用。code

注意:路由参数不能包含 - 字符,须要的话能够使用 _ 替代。blog

  • 可选参数
1 Route::get('/test_param/{id}/{name?}', 'TestSomethingController@testParam');
class TestSomethingController extends Controller
{
    //
    public function testParam($id,$name='defaultName')
    {
        echo $id."==>".$name;
    }
}

你可能想到了,不错,可选参数只能位于路径的末尾。否则laravel就蒙逼了,你到底要请求什么接口?接口

相关文章
相关标签/搜索