php - Can route model binding be used with RESTful controllers? -
i have been using restful controllers in laravel project. including:
route::controller('things', 'thingcontroller') in routes.php, can define functions in thingcontroller like:
public function getdisplay($id) { $thing = thing::find($id) ... } so getting url "...things/display/1" automatically directed controller function. seems pretty handy , has been working great me far.
i noticed many of controller functions start getting model id url, , thought nice able utilize route model binding me instead. updated routes.php to
route::model('thing', 'thing'); route::controller('things', 'thingcontroller') and changed thingcontroller functions to
public function getdisplay($thing) { ... } i assumed magically work way wanted (like else i've tried far in laravel has) unfortunately "trying property of non-object" when effort utilize $thing in function. should able work , have done wrong, or can route model binding work routes explicitly named in routes.php?
if don't mind uri path, method name , work show, edit , update method, can utilize resource controller generate uri string can define model binding.
in routes.php alter to
route::model('things', 'thing'); route::resource('things', 'thingcontroller'); you can utilize php artisan routes command see uris
$ artisan routes | grep thingcontroller get|head things | things.index | thingcontroller@index get|head things/create | things.create | thingcontroller@create post things | things.store | thingcontroller@store get|head things/{things} | things.show | thingcontroller@show get|head things/{things}/edit | things.edit | thingcontroller@edit set things/{things} | things.update | thingcontroller@update patch things/{things} | | thingcontroller@update after can threat parameter thing object without explicitly name route.
/** * display specified thing. * * @param thing $thing * @return mixed */ public function show(thing $thing) { homecoming $thing->tojson(); } if want access thingcontroller@show, pass model id , laravel retrieve automatically.
http://example.com/things/1
{"id":1,"type":"yo!"} php laravel-4 laravel-routing
No comments:
Post a Comment