2014-06-12 83 views
5

是什麼靜態路由方法之間的 「資源」 和 「控制器」laravel 4:Route類資源和控制器之間的差異

Route::controller() 

Route::resource() 

感謝差異,

+0

[Laravel 4的可能重複的 - 路線::資源VS路線::ç ontroller。哪個使用?](http://stackoverflow.com/questions/19102534/laravel-4-routeresource-vs-routecontroller-which-to-use) – Vucko

+0

我認爲這裏有一些區別,當request/url/create時, Route :: resource request create()方法,但Route :: controller請求getCreate()方法 – mwafi

回答

6

我得到的東西:

Route::resource() 
  • 強迫你使用默認方法(索引,創建,存儲,s怎麼樣,編輯,更新,銷燬)沒有辦法在控制器類添加新的方法(沒有辦法調用新的方法)

Route::controller() 
  • 讓你到裏面定義無限的方法
  • 控制器類需要之前函數名稱來定義的HTTP動詞等(postCreate,anyCreate)
0

此方法自動檢測「GET」,「POST」,「PUT/PATCH」,「DELETE」方法。

Route::resource() 

這種方法自動檢測參數由URL

Route::controller() 

也看看吧:Laravel 4 : Route to localhost/controller/action

+0

我認爲這兩種方式都以相同的方式進行交互(使用HTTP動詞) – mwafi

1

您可以在官方文檔中讀到這樣的:

http://laravel.com/docs/controllers#restful-controllers

Route::controller() 

它將聲明所有路線定義爲功能開始對HTML動詞,從文檔例如:

Route::controller('users', 'UserController'); 

    class UserController extends BaseController { 

    public function getIndex() 
    { 
    // 
    } 

    public function postProfile() 
    { 
    // 
    } 

    public function anyLogin() 
    { 
    // 
    } 

} 

在另一方面:

http://laravel.com/docs/controllers#resource-controllers

Route::resource() 

基本上是用來當你使用工匠的創建控制器命令:

php artisan controller:make PhotoController 

它會生成由artisan命令生成的所有路由,基本上是crud路由。

希望它可以幫助你。

1

這裏的發生,當你做兩個路由:

Route::controller('test', 'TestController'); 
Route::resource('othertest', 'OtherTestController'); 

這裏是什麼,我對文字寫出來給你,如果它是任何容易圖片: "php artisan routes" result of the above routes

的以下是一個全功能於一身。例如,如果您使用GETlaravel_dir/test/page,它將在TestController中查找方法getPage()。如果您POSTlaravel_dir/test/page,它會尋找postPage()

URI:GET | HEAD | POST | PUT | PATCH |刪除測試/ {_缺失}

路由名稱:無

行動:@的TestController missingMethod

下面是來自資源路由的結果...您會看到它對於您的routes.php文件的一行中的CRUD非常有用。

URI:GET | HEAD othertest

路由名稱:othertest.index

行動:OtherTestController @指數


URI:GET | HEAD其他/創建

路由名稱:othertest.create

行動:OtherTestController @創建


URI:POST othertest

路由名稱:othertest.store

行動:OtherTestController @店


URI:GET | HEAD othertest/{} othertest

路由名稱:othertest.show

行動:OtherTestController @顯示


URI:GET | HEAD othertest/{} othertest /編輯

路由名稱:othertest。編輯

行動:OtherTestController @編輯


URI:PUT othertest/{} othertest

路由名稱:othertest.update

行動:OtherTestController @update


URI:PATCH othertest/{othertest}

路由名稱:othertest.update(股與上述名稱)

行動:OtherTestController @更新


URI:DELETE othertest/{} othertest

路由名稱:othertest.destroy

行動:OtherTestController @破壞

相關問題