PHP + Zend 框架实战:从零构建 RESTful API(含 CRUD 操作教程)
一. RESTful API 简介
RESTful API 是现代 Web 应用的重要组成部分,它为客户端与后端服务的交互提供了标准化的解决方案。本教程将详细介绍如何使用 PHP 和 Zend 框架 创建 RESTful API,包括开发环境配置、新建项目及实现基础 CRUD 功能。让我们一步步开始吧!
二. 配置开发环境
在开始创建 API 之前,请确保您的系统已安装以下工具:
- PHP
- Composer
安装完毕后,运行以下命令以全局安装 Zend 框架:
composer global require zendframework/zendframework
三. 创建 Zend 框架项目
开发环境配置完成后,通过以下命令创建新的 Zend 框架项目:
composer create-project zendframework/skeleton-application my-api
命令将在当前目录下创建 my-api
项目,并初始化 Zend 框架项目。进入项目目录并启动 PHP 内置服务器:
cd my-api
php -S localhost:8080 -t public
在浏览器访问 http://localhost:8080
,如果看到 Zend 框架欢迎页面,则项目创建成功,可以开始开发 API。
四. 实现简易 RESTful API
我们将创建一个图书管理 API,支持以下操作:
GET /books
– 获取图书列表GET /books/{id}
– 获取指定图书POST /books
– 创建图书PUT /books/{id}
– 更新图书DELETE /books/{id}
– 删除图书
1. 创建 Book 模型
在 module/Application/src/Model
目录下创建 Book.php
:
<?php
namespace Application\Model;
class Book {
public $id;
public $title;
public $author;
public function exchangeArray(array $data) {
$this->id = isset($data['id']) ? $data['id'] : null;
$this->title = isset($data['title']) ? $data['title'] : null;
$this->author = isset($data['author']) ? $data['author'] : null;
}
}
2. 创建 BookTable 类
在同目录下创建 BookTable.php
:
<?php
namespace Application\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class BookTable {
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
return $this->tableGateway->select();
}
public function getBook($id) {
$id = (int) $id;
$rowset = $this->tableGateway->select(['id' => $id]);
$row = $rowset->current();
if (!$row) {
throw new RuntimeException(sprintf('未找到 ID 为 %d 的记录', $id));
}
return $row;
}
public function saveBook(Book $book) {
$data = [
'title' => $book->title,
'author' => $book->author,
];
$id = (int) $book->id;
if ($id === 0) {
$this->tableGateway->insert($data);
return;
}
if (!$this->getBook($id)) {
throw new RuntimeException(sprintf('无法更新不存在的图书 ID %d', $id));
}
$this->tableGateway->update($data, ['id' => $id]);
}
public function deleteBook($id) {
$this->tableGateway->delete(['id' => (int) $id]);
}
}
3. 创建 BookController
在 module/Application/src/Controller
创建 BookController.php
:
<?php
namespace Application\Controller;
use Application\Model\Book;
use Application\Model\BookTable;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class BookController extends AbstractRestfulController {
private $bookTable;
public function __construct(BookTable $bookTable) {
$this->bookTable = $bookTable;
}
public function getList() {
$books = $this->bookTable->fetchAll();
return new JsonModel(['books' => $books]);
}
public function get($id) {
$book = $this->bookTable->getBook($id);
return new JsonModel(['book' => $book]);
}
public function create($data) {
$book = new Book();
$book->exchangeArray($data);
$this->bookTable->saveBook($book);
return new JsonModel(['book' => $book]);
}
public function update($id, $data) {
$book = $this->bookTable->getBook($id);
$book->exchangeArray($data);
$this->bookTable->saveBook($book);
return new JsonModel(['book' => $book]);
}
public function delete($id) {
$this->bookTable->deleteBook($id);
return new JsonModel(['message' => '图书已删除']);
}
}
五. 配置路由
在 module/Application/config/module.config.php
的 routes
节点中添加:
'routes' => [
'book' => [
'type' => Segment::class,
'options' => [
'route' => '/books[/:id]',
'defaults' => [
'controller' => Controller\BookController::class,
],
'constraints' => [
'id' => '[0-9]+',
],
],
],
],
六. 测试 API
使用 Postman 或 curl
测试 API:
- 获取图书列表:
GET /books
- 获取指定图书:
GET /books/{id}
- 创建图书:
POST /books
- 更新图书:
PUT /books/{id}
- 删除图书:
DELETE /books/{id}
可根据需要扩展控制器和模型功能。
七. 结语
本文详细演示了如何使用 PHP 和 Zend 框架创建一个简单的 RESTful API。通过此方法,您可以构建易用、可扩展且易于维护的 API。建议进一步阅读 Zend 框架官方文档 以掌握更多 Web 开发最佳实践。
原文链接: https://reintech.io/blog/creating-restful-apis-php-zend-framework
热门API
- 1. AI文本生成
- 2. AI图片生成_文生图
- 3. AI图片生成_图生图
- 4. AI图像编辑
- 5. AI视频生成_文生视频
- 6. AI视频生成_图生视频
- 7. AI语音合成_文生语音
- 8. AI文本生成(中国)