示例#1
0
<?php

include "library.php";
$lib = new Library();
if (isset($_GET['action'])) {
    switch ($_GET['action']) {
        case "getBooks":
            $data = $lib->getBooks();
            break;
        case "greetStudent":
            $data = $lib->greetStudent(filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING));
            break;
        default:
            // PHP 5.4+ only, use code below for earlier versions
            http_response_code(400);
            // PHP < 5.4 use the line below instead
            // header("Status: 400", false, 400);
            $data = array("error" => "bad request");
    }
    header("Content-Type: application/json");
    echo json_encode($data);
}
 /**
  * http://localhost/debug/books-json
  *
  * @return String
  */
 public function getBooksJson()
 {
     # Old school way of getting books using the Library class and books.json
     # We've since updated this method with the Book model class and `books` table
     # Instantiating an object of the Library class
     $library = new Library(app_path() . '/database/books.json');
     # Get the books
     $books = $library->getBooks();
     # Debug
     return Pre::render($books, 'Books');
 }