我在Laravel項目中一直使用RESTful controllers。通過包括:路由模型綁定可以與RESTful控制器一起使用嗎?
Route::controller('things', 'ThingController')
在我的routes.php文件
,我可以定義功能ThingController
,如:
public function getDisplay($id) {
$thing = Thing::find($id)
...
}
,以便獲取URL」 ......事情/顯示/ 1" 將自動成爲指向控制器功能。這看起來非常方便,迄今爲止我一直在爲我工作。
我注意到我的很多控制器功能都是從網址獲得一個id模型開始的,而且我認爲能夠使用route model binding替代我這樣做會很好。所以,我在我的routes.php文件更新到
Route::model('thing', 'Thing');
Route::controller('things', 'ThingController')
,改變了ThingController
功能
public function getDisplay($thing) {
...
}
我以爲這會神奇的工作,我想它的方式(如一切我到目前爲止已經試過在Laravel有),但不幸的是,當我嘗試在函數中使用$thing
時,我得到「嘗試獲取非對象的屬性」。這是應該能夠工作,我剛剛做錯了,或者可以路由模型綁定只能使用routes.php顯式命名的路由?