這裏使用的是Laravel 5post
PHP Laravel的路由比較強悍,但也正因如此,不統一而容易凌亂。好比在路由中能夠直接寫方法操做(破壞封裝啊)學習
如下是我的學習的例子,不供參考測試
/** * 定義一個/hi地址,返回hi的view */ Route::get('/hi', function() { return View::make("hi"); }); /** * 定義一個/hello地址,帶參數,默認參考值爲Robin * 地址訪問如:/hello/myname */ Route::get("/hello/{name?}", function($name = "Robin"){ return "Hello " . $name; }); /** * 定義一個地址:/test/2222 * 使用正則匹配參數id */ Route::get("/test1/{id}", function($id) { return "ID value = " . $id; })->where("id", "\d+"); /** * 定義一個地址:/test2/123/robin * 使用正則匹配多個參數 */ Route::get("/test2/{id}/{name}", function($id, $name){ return "ID = " . $id . ", Name = " . $name; })->where(["id"=>"\d+", "name"=>"[a-zA-Z]+"]); /** * 定義一個/as/my地址,給此路由加一個別名爲mm */ Route::get("/as/my", ["as" => "mm", function(){ // 輸入當前路由的名稱,若是路由沒有給指定別名,返回空值 return Route::currentRouteName(); }]); //------------------------------------------------------------------ // 測試POST提交 //------------------------------------------------------------------ Route::get("/test", function(){ return View::make("test"); }); Route::post("/test3", function(){ // 取得POST的test文本框輸入值 //return $_POST["test"]; // 取得全部POST的內容 //return Input::all(); // 取得指定文本框的輸入值 return Input::get("test"); }); //------------------------------------------------------------------ // 測試預約義PID爲整型 //------------------------------------------------------------------ $router->pattern("pid", "\d+"); Route::get("/test4/{pid}", function($pid) { return "Pattern ID = " . $pid; });
固然了,它還有一些什麼before、after之些的東西,這裏就不寫了。spa
/** * 使用Conntroller */ Route::get("/test5", ["as" => "test5", "uses" => "TestController@index"]); /** * 使用Controller重定向 */ Route:get("/test6", ["as" => "test6", "uses" => "TestController@test6"]); /** * Route的重定向 */ Route::get("test7",function(){ return Redirect::to("test5"); }); /** * 使用Controller取得當前Route的名稱,名稱爲:mytest8 */ Route::get("test8", ["as" => "mytest8", "uses" => "TestController@test8"]); /** * 使用Controller * Controller的方法前須要使用get開頭 */ Route::controller("/my/test", "my\MyTestController"); //------------------------------------------------------------------ // Controller Group // 加入前綴my2,意思是在裏頭的全部路由地址都以my2開頭 // 例如:http://localhost/my/add //------------------------------------------------------------------ Route::group(["prefix" => "my2"], function() { Route::controller("/", "my\MyTest2Controller"); });