Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel RESTful best practices

I'm building a RESTful app with Laravel 4.2, using resource controllers, as described here: https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

The app is an online publishing platform. One part of the app allows clients to upload images and crop the images for different publications. Each image may be tagged with title, artist, description.

Typically, the RESTful GET-index request URL would look like this. HTTP GET: example.com/image/

Each image would be retrieved with a URL like this. HTTP GET: example.com/image/{id}

Image number 3 would be deleted with HTTP DELETE to a URL like this. HTTP DELETE: example.com/image/3

However my RESTful dilemma arises because each image must be stored in pre-defined crop sizes. Thus an image resource will share title, artist and description, and will have representations that must be retrievable in four different sizes: Original, 1024x768, 640x480, 320x240

The expected demand is about 80,000 images per year, resulting in 320,000 separate image files (Original + 3 crops each).

Here is where I'm looking for "best-practice" advice...

What should my Image Model look like to allow clients to address an end-point for easy retrieval of each cropped image?

What would be a good way of handling the common nature of title, artist and description?

What would the URLs look like if I wanted to retrieve Image #3 in Original and in 1024x768: Original: example.com/image/??? 1024x768: example.com/image/???

What would the URL look like if I wanted to DELETE all cropped versions of Image #3: HTTP DELETE: example.com/image/???

Thanks for any thoughts you can share!

like image 420
Ray Paseur Avatar asked Dec 15 '22 17:12

Ray Paseur


2 Answers

I don't know how Laravel works with Restful APIs, but in general the rules for urls in Restful APIs a little bit different.

  • get single object - http://example.com/images/{id} - with method GET
  • get list - http://example.com/images - with method GET
  • insert/create a new object - http://example.com/images - with method POST
  • DELETE - http://example.com/images/{id} - with method DELETE
  • PUT - http://example.com/images/{id} - with method PUT
  • get single object with some parameters - http://example.com/images/{id}?type=small - with method GET

Also I found this article very usefull.

like image 91
xurshid29 Avatar answered Dec 17 '22 07:12

xurshid29


I would argue that the thumbnails are sub-resources of the Image resource; perhaps a Thumbnail resource? Therefore, you could have a URL structure something like the following:

  • http://example.com/image/{id}
  • http://example.com/image/{id}/thumbnail/{small|medium|large}

Nested resource controllers are achievable in Laravel: http://laravel.com/docs/4.2/controllers#restful-resource-controllers (search for the heading “Handling Nested Resource Controllers”).

This way, you could manipulate individual thumbnail resources, but also images (and any thumbnails) by issuing requests the parent Image resource—just set up an listener on the Image model to delete child Thumbnail resources first when deleting an Image resource. Something like this:

Image::deleting(function($image)
{
    Thumbnail::where('image_id', '=', $image->id)->delete();
});
like image 40
Martin Bean Avatar answered Dec 17 '22 05:12

Martin Bean