教程 7:创建简单的 REST API(Tutorial 7: Creating a Simple REST API)

在这个教程中,我们会学习如何创建一个拥有 RESTful API 的应用程序,它将会使用如下的几个 HTTP 方法:

  • GET - 接受、查找数据
  • POST - 添加数据
  • PUT - 更新数据
  • DELETE - 删除数据

定义 API(Defining the API)

这个 API 包含如下方法(Methods)

MethodURLAction
GET/api/robotsRetrieves all robots
GET/api/robots/search/AstroSearches for robots with ‘Astro’ in their name
GET/api/robots/2Retrieves robots based on primary key
POST/api/robotsAdds a new robot
PUT/api/robots/2Updates robots based on primary key
DELETE/api/robots/2Deletes robots based on primary key

创建应用(Creating the Application)

As the application is so simple, we will not implement any full MVC environment to develop it. In this case, we will use a micro application to meet our goal.

The following file structure is more than enough:

  1. my-rest-api/
  2. models/
  3. Robots.php
  4. index.php
  5. .htaccess

First, we need an .htaccess file that contains all the rules to rewrite the URIs to the index.php file, that is our application:

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
  5. </IfModule>

Then, in the index.php file we create the following:

  1. <?php
  2. use Phalcon\Mvc\Micro;
  3. $app = new Micro();
  4. // Define the routes here
  5. $app->handle();

Now we will create the routes as we defined above:

  1. <?php
  2. use Phalcon\Mvc\Micro;
  3. $app = new Micro();
  4. // Retrieves all robots
  5. $app->get(
  6. "/api/robots",
  7. function () {
  8. }
  9. );
  10. // Searches for robots with $name in their name
  11. $app->get(
  12. "/api/robots/search/{name}",
  13. function ($name) {
  14. }
  15. );
  16. // Retrieves robots based on primary key
  17. $app->get(
  18. "/api/robots/{id:[0-9]+}",
  19. function ($id) {
  20. }
  21. );
  22. // Adds a new robot
  23. $app->post(
  24. "/api/robots",
  25. function () {
  26. }
  27. );
  28. // Updates robots based on primary key
  29. $app->put(
  30. "/api/robots/{id:[0-9]+}",
  31. function () {
  32. }
  33. );
  34. // Deletes robots based on primary key
  35. $app->delete(
  36. "/api/robots/{id:[0-9]+}",
  37. function () {
  38. }
  39. );
  40. $app->handle();

Each route is defined with a method with the same name as the HTTP method, as first parameter we pass a route pattern, followed by a handler. In this case, the handler is an anonymous function. The following route: '/api/robots/{id:[0-9]+}', by example, explicitly sets that the “id” parameter must have a numeric format.

When a defined route matches the requested URI then the application executes the corresponding handler.

创建模型(Creating a Model)

Our API provides information about ‘robots’, these data are stored in a database. The following model allows us to access that table in an object-oriented way. We have implemented some business rules using built-in validators and simple validations. Doing this will give us the peace of mind that saved data meet the requirements of our application:

  1. <?php
  2. namespace Store\Toys;
  3. use Phalcon\Mvc\Model;
  4. use Phalcon\Mvc\Model\Message;
  5. use Phalcon\Mvc\Model\Validator\Uniqueness;
  6. use Phalcon\Mvc\Model\Validator\InclusionIn;
  7. class Robots extends Model
  8. {
  9. public function validation()
  10. {
  11. // Type must be: droid, mechanical or virtual
  12. $this->validate(
  13. new InclusionIn(
  14. [
  15. "field" => "type",
  16. "domain" => [
  17. "droid",
  18. "mechanical",
  19. "virtual",
  20. ]
  21. )
  22. )
  23. );
  24. // Robot name must be unique
  25. $this->validate(
  26. new Uniqueness(
  27. [
  28. "field" => "name",
  29. "message" => "The robot name must be unique",
  30. ]
  31. )
  32. );
  33. // Year cannot be less than zero
  34. if ($this->year < 0) {
  35. $this->appendMessage(
  36. new Message("The year cannot be less than zero")
  37. );
  38. }
  39. // Check if any messages have been produced
  40. if ($this->validationHasFailed() === true) {
  41. return false;
  42. }
  43. }
  44. }

Now, we must set up a connection to be used by this model and load it within our app:

  1. <?php
  2. use Phalcon\Loader;
  3. use Phalcon\Mvc\Micro;
  4. use Phalcon\Di\FactoryDefault;
  5. use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
  6. // Use Loader() to autoload our model
  7. $loader = new Loader();
  8. $loader->registerNamespaces(
  9. [
  10. "Store\\Toys" => __DIR__ . "/models/",
  11. ]
  12. );
  13. $loader->register();
  14. $di = new FactoryDefault();
  15. // Set up the database service
  16. $di->set(
  17. "db",
  18. function () {
  19. return new PdoMysql(
  20. [
  21. "host" => "localhost",
  22. "username" => "asimov",
  23. "password" => "zeroth",
  24. "dbname" => "robotics",
  25. ]
  26. );
  27. }
  28. );
  29. // Create and bind the DI to the application
  30. $app = new Micro($di);

检索数据(Retrieving Data)

The first “handler” that we will implement is which by method GET returns all available robots. Let’s use PHQL to perform this simple query returning the results as JSON:

  1. <?php
  2. // Retrieves all robots
  3. $app->get(
  4. "/api/robots",
  5. function () use ($app) {
  6. $phql = "SELECT * FROM Store\\Toys\\Robots ORDER BY name";
  7. $robots = $app->modelsManager->executeQuery($phql);
  8. $data = [];
  9. foreach ($robots as $robot) {
  10. $data[] = [
  11. "id" => $robot->id,
  12. "name" => $robot->name,
  13. ];
  14. }
  15. echo json_encode($data);
  16. }
  17. );

PHQL, allow us to write queries using a high-level, object-oriented SQL dialect that internally translates to the right SQL statements depending on the database system we are using. The clause “use” in the anonymous function allows us to pass some variables from the global to local scope easily.

The searching by name handler would look like:

  1. <?php
  2. // Searches for robots with $name in their name
  3. $app->get(
  4. "/api/robots/search/{name}",
  5. function ($name) use ($app) {
  6. $phql = "SELECT * FROM Store\\Toys\\Robots WHERE name LIKE :name: ORDER BY name";
  7. $robots = $app->modelsManager->executeQuery(
  8. $phql,
  9. [
  10. "name" => "%" . $name . "%"
  11. ]
  12. );
  13. $data = [];
  14. foreach ($robots as $robot) {
  15. $data[] = [
  16. "id" => $robot->id,
  17. "name" => $robot->name,
  18. ];
  19. }
  20. echo json_encode($data);
  21. }
  22. );

Searching by the field “id” it’s quite similar, in this case, we’re also notifying if the robot was found or not:

  1. <?php
  2. use Phalcon\Http\Response;
  3. // Retrieves robots based on primary key
  4. $app->get(
  5. "/api/robots/{id:[0-9]+}",
  6. function ($id) use ($app) {
  7. $phql = "SELECT * FROM Store\\Toys\\Robots WHERE id = :id:";
  8. $robot = $app->modelsManager->executeQuery(
  9. $phql,
  10. [
  11. "id" => $id,
  12. ]
  13. )->getFirst();
  14. // Create a response
  15. $response = new Response();
  16. if ($robot === false) {
  17. $response->setJsonContent(
  18. [
  19. "status" => "NOT-FOUND"
  20. ]
  21. );
  22. } else {
  23. $response->setJsonContent(
  24. [
  25. "status" => "FOUND",
  26. "data" => [
  27. "id" => $robot->id,
  28. "name" => $robot->name
  29. ]
  30. ]
  31. );
  32. }
  33. return $response;
  34. }
  35. );

插入数据(Inserting Data)

Taking the data as a JSON string inserted in the body of the request, we also use PHQL for insertion:

  1. <?php
  2. use Phalcon\Http\Response;
  3. // Adds a new robot
  4. $app->post(
  5. "/api/robots",
  6. function () use ($app) {
  7. $robot = $app->request->getJsonRawBody();
  8. $phql = "INSERT INTO Store\\Toys\\Robots (name, type, year) VALUES (:name:, :type:, :year:)";
  9. $status = $app->modelsManager->executeQuery(
  10. $phql,
  11. [
  12. "name" => $robot->name,
  13. "type" => $robot->type,
  14. "year" => $robot->year,
  15. ]
  16. );
  17. // Create a response
  18. $response = new Response();
  19. // Check if the insertion was successful
  20. if ($status->success() === true) {
  21. // Change the HTTP status
  22. $response->setStatusCode(201, "Created");
  23. $robot->id = $status->getModel()->id;
  24. $response->setJsonContent(
  25. [
  26. "status" => "OK",
  27. "data" => $robot,
  28. ]
  29. );
  30. } else {
  31. // Change the HTTP status
  32. $response->setStatusCode(409, "Conflict");
  33. // Send errors to the client
  34. $errors = [];
  35. foreach ($status->getMessages() as $message) {
  36. $errors[] = $message->getMessage();
  37. }
  38. $response->setJsonContent(
  39. [
  40. "status" => "ERROR",
  41. "messages" => $errors,
  42. ]
  43. );
  44. }
  45. return $response;
  46. }
  47. );

更新数据(Updating Data)

The data update is similar to insertion. The “id” passed as parameter indicates what robot must be updated:

  1. <?php
  2. use Phalcon\Http\Response;
  3. // Updates robots based on primary key
  4. $app->put(
  5. "/api/robots/{id:[0-9]+}",
  6. function ($id) use ($app) {
  7. $robot = $app->request->getJsonRawBody();
  8. $phql = "UPDATE Store\\Toys\\Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:";
  9. $status = $app->modelsManager->executeQuery(
  10. $phql,
  11. [
  12. "id" => $id,
  13. "name" => $robot->name,
  14. "type" => $robot->type,
  15. "year" => $robot->year,
  16. ]
  17. );
  18. // Create a response
  19. $response = new Response();
  20. // Check if the insertion was successful
  21. if ($status->success() === true) {
  22. $response->setJsonContent(
  23. [
  24. "status" => "OK"
  25. ]
  26. );
  27. } else {
  28. // Change the HTTP status
  29. $response->setStatusCode(409, "Conflict");
  30. $errors = [];
  31. foreach ($status->getMessages() as $message) {
  32. $errors[] = $message->getMessage();
  33. }
  34. $response->setJsonContent(
  35. [
  36. "status" => "ERROR",
  37. "messages" => $errors,
  38. ]
  39. );
  40. }
  41. return $response;
  42. }
  43. );

删除数据(Deleting Data)

The data delete is similar to update. The “id” passed as parameter indicates what robot must be deleted:

  1. <?php
  2. use Phalcon\Http\Response;
  3. // Deletes robots based on primary key
  4. $app->delete(
  5. "/api/robots/{id:[0-9]+}",
  6. function ($id) use ($app) {
  7. $phql = "DELETE FROM Store\\Toys\\Robots WHERE id = :id:";
  8. $status = $app->modelsManager->executeQuery(
  9. $phql,
  10. [
  11. "id" => $id,
  12. ]
  13. );
  14. // Create a response
  15. $response = new Response();
  16. if ($status->success() === true) {
  17. $response->setJsonContent(
  18. [
  19. "status" => "OK"
  20. ]
  21. );
  22. } else {
  23. // Change the HTTP status
  24. $response->setStatusCode(409, "Conflict");
  25. $errors = [];
  26. foreach ($status->getMessages() as $message) {
  27. $errors[] = $message->getMessage();
  28. }
  29. $response->setJsonContent(
  30. [
  31. "status" => "ERROR",
  32. "messages" => $errors,
  33. ]
  34. );
  35. }
  36. return $response;
  37. }
  38. );

测试应用(Testing our Application)

Using curl we’ll test every route in our application verifying its proper operation.

Obtain all the robots:

  1. curl -i -X GET http://localhost/my-rest-api/api/robots
  2. HTTP/1.1 200 OK
  3. Date: Tue, 21 Jul 2015 07:05:13 GMT
  4. Server: Apache/2.2.22 (Unix) DAV/2
  5. Content-Length: 117
  6. Content-Type: text/html; charset=UTF-8
  7. [{"id":"1","name":"Robotina"},{"id":"2","name":"Astro Boy"},{"id":"3","name":"Terminator"}]

Search a robot by its name:

  1. curl -i -X GET http://localhost/my-rest-api/api/robots/search/Astro
  2. HTTP/1.1 200 OK
  3. Date: Tue, 21 Jul 2015 07:09:23 GMT
  4. Server: Apache/2.2.22 (Unix) DAV/2
  5. Content-Length: 31
  6. Content-Type: text/html; charset=UTF-8
  7. [{"id":"2","name":"Astro Boy"}]

Obtain a robot by its id:

  1. curl -i -X GET http://localhost/my-rest-api/api/robots/3
  2. HTTP/1.1 200 OK
  3. Date: Tue, 21 Jul 2015 07:12:18 GMT
  4. Server: Apache/2.2.22 (Unix) DAV/2
  5. Content-Length: 56
  6. Content-Type: text/html; charset=UTF-8
  7. {"status":"FOUND","data":{"id":"3","name":"Terminator"}}

Insert a new robot:

  1. curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}'
  2. http://localhost/my-rest-api/api/robots
  3. HTTP/1.1 201 Created
  4. Date: Tue, 21 Jul 2015 07:15:09 GMT
  5. Server: Apache/2.2.22 (Unix) DAV/2
  6. Content-Length: 75
  7. Content-Type: text/html; charset=UTF-8
  8. {"status":"OK","data":{"name":"C-3PO","type":"droid","year":1977,"id":"4"}}

Try to insert a new robot with the name of an existing robot:

  1. curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}'
  2. http://localhost/my-rest-api/api/robots
  3. HTTP/1.1 409 Conflict
  4. Date: Tue, 21 Jul 2015 07:18:28 GMT
  5. Server: Apache/2.2.22 (Unix) DAV/2
  6. Content-Length: 63
  7. Content-Type: text/html; charset=UTF-8
  8. {"status":"ERROR","messages":["The robot name must be unique"]}

Or update a robot with an unknown type:

  1. curl -i -X PUT -d '{"name":"ASIMO","type":"humanoid","year":2000}'
  2. http://localhost/my-rest-api/api/robots/4
  3. HTTP/1.1 409 Conflict
  4. Date: Tue, 21 Jul 2015 08:48:01 GMT
  5. Server: Apache/2.2.22 (Unix) DAV/2
  6. Content-Length: 104
  7. Content-Type: text/html; charset=UTF-8
  8. {"status":"ERROR","messages":["Value of field 'type' must be part of
  9. list: droid, mechanical, virtual"]}

Finally, delete a robot:

  1. curl -i -X DELETE http://localhost/my-rest-api/api/robots/4
  2. HTTP/1.1 200 OK
  3. Date: Tue, 21 Jul 2015 08:49:29 GMT
  4. Server: Apache/2.2.22 (Unix) DAV/2
  5. Content-Length: 15
  6. Content-Type: text/html; charset=UTF-8
  7. {"status":"OK"}

结束语(Conclusion)

As we have seen, develop a RESTful API with Phalcon is easy. Later in the documentation we’ll explain in detail how to use micro applications and the PHQL language.