Laravel 路由
使用了 Laravel 一段時間,將其心得分享紀錄在此,供網友參考
(1).註冊一個基本的 route 透過 get 方法傳送至 根目錄( / ) 得到的回應
直接反回字串
Route::get('/', function()
{
return 'Hello World';
});
(2).也可以透過 post 或是 any (GET、POST、PUT及DELETE)
Route::get('/con/get', function()
{
return 'Hello World';
});
Route::any('/con/any', function()
{
return 'Hello World';
});
當code確實打完後,應該可以在瀏覽器輸入
http://your.ip(or domain)/con/get 看到結果
(3)路由亦可以夾帶參數
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
(4).有預設值的路由參數
Route::get('user/{name?}', function($name = 'John')
{
return $name;
});
?表該項參數可選擇亦可不填寫
(5).自行設計條件過濾
Route::get('user/{age?}', function($age=null)
{
if( $age >200 )
{ ........ }
else
{ ........ }
}));
(6)過濾條件亦可透過路由過濾
在 app/filter.php內
Route::filter('old', function()
{
if (Input::get('age') > 200)
{
{......}
}else{
{......}
}
});
在app/route.php內
Route::get('user', array('before' => 'old', function()
{
return 'You are over 200 years old!';
}));
在瀏覽器輸入以下狀況時可以將其過濾
(7).路由前綴
Route::group(array("prefix" => "/api/") , function()
{
Route::any("fun1" , function()
{
return View::make("fun1");
});
Route::any("fun2" , function()
{
return View::make("fun2");
});
});
這時瀏覽網址便可輸入
http://nl.rg-laravel.com/api/fun1
http://nl.rg-laravel.com/api/fun2
(8).透過路由指向一個視圖頁面
這裡指的視圖就是view,可以把它想像成網站靜態頁面
該login會連結到 app/view/login.php
Route::any("login" , function()
{
return View::make("login");
});
(9).輸入參數接值傳遞至view
可以觀察到路由後填寫的名子與其後變數名不用相同
Route::any("api2/{name}/{age}" , function($name1,$age1)
{
$c1 = $name1;
$c2 = $age1;
return View::make("rgMain",array(
'rgFactor' => $c1 ,
'rgContent' => $c2
));
});
(10).透過路由來控制 Controller與其方法使用
以下內容表當路由連到 LoginCheck 會去呼叫 LoginCheckController 裡的 check 方法
Route::post('/LoginCheck', 'LoginCheckController@check )
(11).路由回應 json格式內容
Route::any("OpenList/Show/{gameName?}/{deskName?}" , function($gameName=null, $deskName=null)
{
return Response::json(
array(
array("Round" => "0120150129001" ,
"Inning" => "0001" ,
"One" => "11" ,
"Two" => "12" ,
"Three" => "13" ,
"Four" => "14" ,
"Five" => "15" ,
"Six" => "16" ,
"Player" => "0" ,
"Banker" => "0" ,
"Winner" => "Tie") ,
array("Round" => "0120150129001" ,
"Inning" => "002" ,
"One" => "21" ,
"Two" => "22" ,
"Three" => "23" ,
"Four" => "24" ,
"Five" => "25" ,
"Six" => "26" ,
"Player" => "1" ,
"Banker" => "1" ,
"Winner" => "Player") ,
)
) ;
});
(12).路由實作cache
Route::get('api/test/openlist/cache/{limit?}' , function($limit = 50)
{
$key = "k78ab1845";
$content = Cache::get($key) ;
if( $content )
{
// Cache::forget($cql) ;
echo "has cache = </br>" ;
}
else
{
echo "not cache = </br>" ;
$content = DB::table("OpenList")->take($limit)->get();
Cache::forever($key , $content) ;
}
return Response::json($content) ;
});
(13)404事件
App::missing(function($exception)
{
return Redirect::to('http://');
});
(14)利用路由對資料庫做查詢回應結果內容
Route::get('api/test/openlist/get/{limit?}' , function($limit = 50)
{
$result = DB::select("SELECT * FROM OpenList where id = ?" , array(1) );
return Response::json($result) ;
});
Route::get('api/test/openlist/get/{limit?}' , function($limit = 50)
{
$result = DB::select("SELECT * FROM OpenList where id = ? AND serverid = ?" , array(1,54671) );
//欲查詢變數順序,依array內容順序
return Response::json($result) ;
});
關於路由對資料庫操作會另一寫一個篇幅做介紹
沒有留言:
張貼留言