06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

HTTP Controllers 141<br />

21 }<br />

22 }<br />

RESTful Resource Controllers<br />

Resource controllers make it painless to build RESTful controllers around resources. For example,<br />

you may wish to create a controller that handles HTTP requests regarding “photos” stored by your<br />

application. Using the make:controller Artisan command, we can quickly create such a controller:<br />

1 php artisan make:controller PhotoController --resource<br />

The Artisan command will generate a controller file at app/Http/Controllers/PhotoController.php.<br />

The controller will contain a method for each of the available resource operations.<br />

Next, you may register a resourceful route to the controller:<br />

1 Route::resource('photos', 'PhotoController');<br />

This single route declaration creates multiple routes to handle a variety of RESTful actions on the<br />

photo resource. Likewise, the generated controller will already have methods stubbed for each of<br />

these actions, including notes informing you which URIs and verbs they handle.<br />

Actions Handled By Resource Controller<br />

| Verb | Path | Action | Route Name | ———-|———————–|————–|——————— | GET | /photos<br />

| index | photos.index | | GET | /photos/create | create | photos.create | | POST | /photos | store |<br />

photos.store | | GET | /photos/{photo} | show | photos.show | | GET | /photos/{photo}/edit | edit |<br />

photos.edit | | PUT/PATCH | /photos/{photo} | update | photos.update | | DELETE | /photos/{photo}<br />

| destroy | photos.destroy |<br />

Remember, since HTML forms can’t make PUT, PATCH, or DELETE requests, you will need to add<br />

a hidden _method field to spoof these HTTP verbs:<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!