[Laravel] Responses and Codes

1. update app/controllers/LessonsController.php
check http status code

public function index()
{
  // 1. All is bad
  // 2. No way to attach meta data
  // 3. Linking db structure to the API output
  // 4. No way to signal headers/response codes
    
  //return Lesson::all();
  
  $lessons = Lesson::all();
  return Response::json([
    'data' => $lessons->toArray()
  ], 200);  //200 also can use 404(not found)
}

2. use curl to test

$ curl -i http://localhost:8000/api/v1/lessons
// or you can use "python -mjson.tool" to show more human readable
$ curl http://localhost:8000/api/v1/lessons | python -mjson.tool

Screen Shot 2014-05-17 at 8.31.50 AM

3. update show function to test specific id
try: http://localhost:8000/api/v1/lessons/5 or try: http://localhost:8000/api/v1/lessons/50

public function show($id)
{
  $lesson = Lesson::find($id);
  if( ! $lesson)
  {
    return Response::json([
      'error' => [
        'message' => 'Lesson does not exist'
      ]
    ], 404);
  }
    
  return Response::json([
    'data' => $lesson->toArray()
  ], 200);
}