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就蒙逼了,你到底要請求什麼接口?接口

相關文章
相關標籤/搜索