Laravel 4路由是一種支持RESTful的路由體系, 基於symfony2的Routing組件構成,語法簡潔明瞭,功能強大。關於RESTful,參考理解RESTful架構這篇文章。Laravel應用中的大多數路都會定義在app/routes.php文件中。php

基本路由

最基本的Laravel路由由URI和閉包回調函數(匿名函數)組成。第二個參數能夠是一個匿名函數,也能夠是一個數組,用於指定過濾器或是HTTPS協議等html

  1. Route::get('my/page',function(){
  2. return'Hello world!';
  3. });

當URL以GET方式訪問http://localhost/my/page時,將返回Hello world!字樣。Route支持如下方法捕捉不一樣的HTTP動做laravel

  1. Route::get();
  2. Route::post();
  3. Route::put();
  4. Route::delete();
  5. Route::any();

帶參數的路由

帶參數的路由能夠很容易將一個值經過URL傳遞給程序,好比正則表達式

  1. Route::get('/books/{genre}',function($genre)
  2. {
  3. return"Books in the {$genre} category.";
  4. });

可選路由參數json

  1. Route::get('/books/{genre?}',function($genre =null)
  2. {
  3. if($genre ==null)return'Books index.';
  4. return"Books in the {$genre} category.";
  5. });

帶默認值的路由參數數組

  1. Route::get('/books/{genre?}',function($genre ='Crime')
  2. {
  3. return"Books in the {$genre} category.";
  4. });

支持HTTPS的安全路由

  1. Route::get('secret/content', array(
  2. 'https',
  3. function(){
  4. return'Secret squirrel!';
  5. }
  6. ));

帶有正則表達式約束條件的路由

  1. Route::get('save/{princess}',function($princess)
  2. {
  3. return"Sorry, {$princess} is in another castle. :(";
  4. })->where('princess','[A-Za-z]+');

多個條件限定安全

  1. Route::get('save/{princess}/{unicorn}',function($princess, $unicorn)
  2. {
  3. return"{$princess} loves {$unicorn}";
  4. })->where('princess','[A-Za-z]+')
  5. ->where('unicorn','[0-9]+');

路由過濾器

過濾器能夠在路由以前或以後進行相關的邏輯判斷和動做,確保你有權限訪問相應的資源。過濾器在app/filters.php中定義restful

  1. // app/filters.php
  2. Route::filter('birthday',function()
  3. {
  4. if(date('d/m')=='16/08'){
  5. return'Happy birthday';
  6. }
  7. });

在路由前綁定過濾器閉包

  1. // app/routes.php
  2. Route::get('/', array(
  3. 'before'=>'birthday',
  4. function()
  5. {
  6. return'hello word!';
  7. }
  8. ));

若是當天爲16/08,那麼輸出'Happy birthday',不然輸出'hello word!',一旦過濾器有返回響應,則中止路由。過濾器沒有返回則路由繼續。架構

也能夠在路由後綁定過濾器

  1. // app/routes.php
  2. Route::get('/', array(
  3. 'after'=>'birthday',
  4. function()
  5. {
  6. return'hello word!';
  7. }
  8. ));

綁定多個過濾器

  1. // app/routes.php
  2. Route::get('/', array(
  3. 'before'=>'birthday|christmas',
  4. function()
  5. {
  6. returnView::make('hello');
  7. }
  8. ));

過濾器從左到右依次執行,若是有一個返回響應,則請求終止,執行返回的響應。也能夠用數組的形式

  1. // app/routes.php
  2. Route::get('/', array(
  3. 'before'=> array('birthday','christmas'),
  4. function()
  5. {
  6. returnView::make('hello');
  7. }
  8. ));

過濾器參數

before過濾器的function默認兩個參數,after過濾器默認爲三個

  1. // before
  2. Route::filter('test',function($route, $request)
  3. {
  4. });
  5. // after
  6. Route::filter('test',function($route, $request, $response)
  7. {
  8. });

所以before過濾器的第三個參數之後爲用戶自定義參數,after第四個參數之後爲用戶自定義參數

  1. // app/filters.php
  2. Route::filter('birthday',function($route, $request, $date)
  3. {
  4. if(date('d/m')== $date){
  5. return'Happy birthday';
  6. }
  7. });

路由中經過過濾器名後加:號添加參數

  1. Route::get('/', array(
  2. 'before'=>'birthday:16/08',
  3. function()
  4. {
  5. return'hello word!';
  6. }
  7. ));

多個過濾器參數

  1. // app/filters.php
  2. Route::filter('birthday',function($route, $request, $first, $second, $third)
  3. {
  4. return"{$first} - {$second} - {$third}";
  5. });
  6. // app/routes.php
  7. Route::get('/', array(
  8. 'before'=>'birthday:foo,bar,baz',
  9. function()
  10. {
  11. return'hello word!';
  12. }
  13. ));

過濾器類

咱們能夠用過濾器類代替閉包函數,方便之後測試,過濾器類能夠在app/filters.php中定義,也能夠放在任何地方,假設咱們在/app目錄下新建一個filters文件夾,專門用來放過濾器類,那麼咱們必須先更改composer.json文件,將新的目錄添加進類自動加載'classmap'中

  1. "autoload":{
  2. "classmap":[
  3. "app/commands",
  4. "app/controllers",
  5. "app/models",
  6. "app/filters",
  7. "app/database/migrations",
  8. "app/database/seeds",
  9. "app/tests/TestCase.php"
  10. ]
  11. }

而後建立過濾器類文件

  1. <?php
  2. // app/filters/Birthday.php
  3. classBirthdayFilter
  4. {
  5. publicfunction filter($route, $request, $date='16/08')
  6. {
  7. if(date('d/m')== $date){
  8. return'Happy bitrhday';
  9. }
  10. }
  11. }

類名稱沒有特別約束,主要是實現filter()函數,而後註冊咱們的過濾器類

  1. // app/filters.php
  2. Route::filter('birthday','BirthdayFilter');

而後跟路由綁定

  1. // app/routes.php
  2. Route::get('/', array(
  3. 'before'=>'birthday',
  4. function()
  5. {
  6. return'hello word';
  7. }
  8. ));

在瀏覽以前,需運行composer dump-autoload,更新自動加載文件,使其能找到咱們建立的類。

全局過濾器

app/filters.php中有兩個全局過濾器,適用於任何請求

  1. App::before(function($request)
  2. {
  3. //
  4. });
  5. App::after(function($request, $response)
  6. {
  7. //
  8. });

此外app/filters.php文件中還定義了auth,auth.basic,guest,csrf四個默認過濾器

模式過濾器

能夠針對一組路由綁定過濾器

  1. // app/routes.php
  2. Route::when('profile/*','birthday');

或是根據HTTP動做限定過濾器

  1. // app/routes.php
  2. Route::when('profile/*','birthday', array('post'));

命名路由

能夠爲較長較複雜的路由命名一個簡單的名字,方便重定向或,生成URL

  1. // app/routes.php
  2. Route::get('/my/long/calendar/route', array(
  3. 'as'=>'calendar',
  4. function(){
  5. return route('calendar');
  6. }
  7. ));

使用路由名稱來建立URL和重定向

  1. $url = URL::route('calendar');
  2. $redirect =Redirect::route('calendar');

獲取當前路由的別名

  1. $current =Route::currentRouteName();

還能夠爲控制器指定路由名稱

  1. // app/routes.php
  2. Route::get('/my/long/calendar/route', array(
  3. 'as'=>'calendar',
  4. 'uses'=>'CalendarController@showCalendar'
  5. ));

路由組

前面經過Route::when()爲一組相同路由綁定過濾器,若是要爲多個不一樣組的路由綁定,則須要路由組Route::group()

  1. // app/routes.php
  2. Route::group(array('before'=>'onlybrogrammers'),function()
  3. {
  4. // First Route
  5. Route::get('/first',function(){
  6. return'Dude!';
  7. });
  8. // Second Route
  9. Route::get('/second',function(){
  10. return'Duuuuude!';
  11. });
  12. });

路由前綴

爲組路由設置前綴

  1. // app/routes.php
  2. Route::group(array('prefix'=>'books'),function()
  3. {
  4. // First Route
  5. Route::get('/first',function(){
  6. return'The Colour of Magic';
  7. });
  8. // Second Route
  9. Route::get('/second',function(){
  10. return'Reaper Man';
  11. });
  12. });

這樣能夠經過localhost/books/first形式訪問

子域名路由

經過子域名的不一樣訪問不一樣的資源,好比下面地址

  1. http://myapp.dev/my/route
  2. http://another.myapp.dev/my/route
  3. http://third.myapp.dev/my/route

路由以下

  1. // app/routes.php
  2. Route::group(array('domain'=>'myapp.dev'),function()
  3. {
  4. Route::get('my/route',function(){
  5. return'Hello from myapp.dev!';
  6. });
  7. });
  8. Route::group(array('domain'=>'another.myapp.dev'),function()
  9. {
  10. Route::get('my/route',function(){
  11. return'Hello from another.myapp.dev!';
  12. });
  13. });

一樣,子域名裏還能夠傳遞參數

  1. Route::group(array('domain'=>'{user}.myapp.dev'),function()
  2. {
  3. Route::get('profile/{page}',function($user, $page){
  4. // ...
  5. });
  6. });

結束

Laravel提供的路由功能不只這些,還包括控制器路由,路由跟模型的綁定,甚至支持建立resource controllers。