Laravel 4路由是一種支持RESTful的路由體系, 基於symfony2的Routing組件構成,語法簡潔明瞭,功能強大。關於RESTful,參考理解RESTful架構這篇文章。Laravel應用中的大多數路都會定義在app/routes.php
文件中。php
基本路由
最基本的Laravel路由由URI和閉包回調函數(匿名函數)組成。第二個參數能夠是一個匿名函數,也能夠是一個數組,用於指定過濾器或是HTTPS協議等html
Route::get('my/page',function(){
return'Hello world!';
});
當URL以GET方式訪問http://localhost/my/page
時,將返回Hello world!字樣。Route支持如下方法捕捉不一樣的HTTP動做laravel
Route::get();
Route::post();
Route::put();
Route::delete();
Route::any();
帶參數的路由
帶參數的路由能夠很容易將一個值經過URL傳遞給程序,好比正則表達式
Route::get('/books/{genre}',function($genre)
{
return"Books in the {$genre} category.";
});
可選路由參數json
Route::get('/books/{genre?}',function($genre =null)
{
if($genre ==null)return'Books index.';
return"Books in the {$genre} category.";
});
帶默認值的路由參數數組
Route::get('/books/{genre?}',function($genre ='Crime')
{
return"Books in the {$genre} category.";
});
支持HTTPS的安全路由
Route::get('secret/content', array(
'https',
function(){
return'Secret squirrel!';
}
));
帶有正則表達式約束條件的路由
Route::get('save/{princess}',function($princess)
{
return"Sorry, {$princess} is in another castle. :(";
})->where('princess','[A-Za-z]+');
多個條件限定安全
Route::get('save/{princess}/{unicorn}',function($princess, $unicorn)
{
return"{$princess} loves {$unicorn}";
})->where('princess','[A-Za-z]+')
->where('unicorn','[0-9]+');
路由過濾器
過濾器能夠在路由以前或以後進行相關的邏輯判斷和動做,確保你有權限訪問相應的資源。過濾器在app/filters.php
中定義restful
// app/filters.php
Route::filter('birthday',function()
{
if(date('d/m')=='16/08'){
return'Happy birthday';
}
});
在路由前綁定過濾器閉包
// app/routes.php
Route::get('/', array(
'before'=>'birthday',
function()
{
return'hello word!';
}
));
若是當天爲16/08,那麼輸出'Happy birthday',不然輸出'hello word!',一旦過濾器有返回響應,則中止路由。過濾器沒有返回則路由繼續。架構
也能夠在路由後綁定過濾器
// app/routes.php
Route::get('/', array(
'after'=>'birthday',
function()
{
return'hello word!';
}
));
綁定多個過濾器
// app/routes.php
Route::get('/', array(
'before'=>'birthday|christmas',
function()
{
returnView::make('hello');
}
));
過濾器從左到右依次執行,若是有一個返回響應,則請求終止,執行返回的響應。也能夠用數組的形式
// app/routes.php
Route::get('/', array(
'before'=> array('birthday','christmas'),
function()
{
returnView::make('hello');
}
));
過濾器參數
before
過濾器的function默認兩個參數,after
過濾器默認爲三個
// before
Route::filter('test',function($route, $request)
{
});
// after
Route::filter('test',function($route, $request, $response)
{
});
所以before
過濾器的第三個參數之後爲用戶自定義參數,after
第四個參數之後爲用戶自定義參數
// app/filters.php
Route::filter('birthday',function($route, $request, $date)
{
if(date('d/m')== $date){
return'Happy birthday';
}
});
路由中經過過濾器名後加:
號添加參數
Route::get('/', array(
'before'=>'birthday:16/08',
function()
{
return'hello word!';
}
));
多個過濾器參數
// app/filters.php
Route::filter('birthday',function($route, $request, $first, $second, $third)
{
return"{$first} - {$second} - {$third}";
});
// app/routes.php
Route::get('/', array(
'before'=>'birthday:foo,bar,baz',
function()
{
return'hello word!';
}
));
過濾器類
咱們能夠用過濾器類代替閉包函數,方便之後測試,過濾器類能夠在app/filters.php
中定義,也能夠放在任何地方,假設咱們在/app
目錄下新建一個filters
文件夾,專門用來放過濾器類,那麼咱們必須先更改composer.json
文件,將新的目錄添加進類自動加載'classmap'中
"autoload":{
"classmap":[
"app/commands",
"app/controllers",
"app/models",
"app/filters",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
}
而後建立過濾器類文件
<?php
// app/filters/Birthday.php
classBirthdayFilter
{
publicfunction filter($route, $request, $date='16/08')
{
if(date('d/m')== $date){
return'Happy bitrhday';
}
}
}
類名稱沒有特別約束,主要是實現filter()
函數,而後註冊咱們的過濾器類
// app/filters.php
Route::filter('birthday','BirthdayFilter');
而後跟路由綁定
// app/routes.php
Route::get('/', array(
'before'=>'birthday',
function()
{
return'hello word';
}
));
在瀏覽以前,需運行composer dump-autoload
,更新自動加載文件,使其能找到咱們建立的類。
全局過濾器
app/filters.php
中有兩個全局過濾器,適用於任何請求
App::before(function($request)
{
//
});
App::after(function($request, $response)
{
//
});
此外app/filters.php
文件中還定義了auth
,auth.basic
,guest
,csrf
四個默認過濾器
模式過濾器
能夠針對一組路由綁定過濾器
// app/routes.php
Route::when('profile/*','birthday');
或是根據HTTP動做限定過濾器
// app/routes.php
Route::when('profile/*','birthday', array('post'));
命名路由
能夠爲較長較複雜的路由命名一個簡單的名字,方便重定向或,生成URL
// app/routes.php
Route::get('/my/long/calendar/route', array(
'as'=>'calendar',
function(){
return route('calendar');
}
));
使用路由名稱來建立URL和重定向
$url = URL::route('calendar');
$redirect =Redirect::route('calendar');
獲取當前路由的別名
$current =Route::currentRouteName();
還能夠爲控制器指定路由名稱
// app/routes.php
Route::get('/my/long/calendar/route', array(
'as'=>'calendar',
'uses'=>'CalendarController@showCalendar'
));
路由組
前面經過Route::when()
爲一組相同路由綁定過濾器,若是要爲多個不一樣組的路由綁定,則須要路由組Route::group()
// app/routes.php
Route::group(array('before'=>'onlybrogrammers'),function()
{
// First Route
Route::get('/first',function(){
return'Dude!';
});
// Second Route
Route::get('/second',function(){
return'Duuuuude!';
});
});
路由前綴
爲組路由設置前綴
// app/routes.php
Route::group(array('prefix'=>'books'),function()
{
// First Route
Route::get('/first',function(){
return'The Colour of Magic';
});
// Second Route
Route::get('/second',function(){
return'Reaper Man';
});
});
這樣能夠經過localhost/books/first
形式訪問
子域名路由
經過子域名的不一樣訪問不一樣的資源,好比下面地址
http://myapp.dev/my/route
http://another.myapp.dev/my/route
http://third.myapp.dev/my/route
路由以下
// app/routes.php
Route::group(array('domain'=>'myapp.dev'),function()
{
Route::get('my/route',function(){
return'Hello from myapp.dev!';
});
});
Route::group(array('domain'=>'another.myapp.dev'),function()
{
Route::get('my/route',function(){
return'Hello from another.myapp.dev!';
});
});
一樣,子域名裏還能夠傳遞參數
Route::group(array('domain'=>'{user}.myapp.dev'),function()
{
Route::get('profile/{page}',function($user, $page){
// ...
});
});
結束
Laravel提供的路由功能不只這些,還包括控制器路由,路由跟模型的綁定,甚至支持建立resource controllers。