webapi php,php 创建简单的Restful WebAPI(二)

本文详细介绍了如何使用PHP构建一个RESTful Web API,包括中央控制器的设计、URL重写配置、路由规则的编写,以及Controller、Request、Response的分工合作。重点讲解了如何利用`route.php`和`controller`结构来处理HTTP请求和响应。
摘要由CSDN通过智能技术生成

php 创建简单的Restful WebAPI(二)

2018-09-14

先说一下这个项目的代码结构吧。

首先是index.php,我是把它作为中央控制器,一个调度器。程序通过rewrite或其他方式,把所有url导向本文件,由index.php调度其他代码。

然后route.php,用设置好的路由规则匹配当前的url,来选择响应的controller(下面解释)来处理request,返回response。

request.php,本文件用来解析requset信息(头部信息:method,accept,querystring,请求body的数据(主要为json格式)等),保存到内存里等待其他程序使用。

response.php,本文件用来生成response信息(body数据(json格式),设置状态码,content-type等),返回给客户端。

controller文件夹内的文件,这些文件用来处理具体业务逻辑,根据request.php,操作数据库,使用response.php生成结果返回给客户端。

db.php由名字可知,本文件为数据库操作类。

还有其他类如日志功能,身份验证等等以后再实现吧。

Rewite

应用程序首先将所有url导向到index.php。可以使用Apache服务器mod_rewrite重写转向(Rewrite)模块来实现。由于我的web服务搭建在新浪SAE,它本身的config.yaml具有url重写功能,只需要加入一句 - rewrite: if(!is_file()) goto "index.php?%{QUERY_STRING}" 就可以了(config.yaml配置可参阅SAE相关文档)。

Route

首先在router.php配置相关规则:

<?php /** *路由配置文件编写说明: * 路由配置在一个array数组中,一条记录代表一个规则 * 优先匹配索引低的规则 * key: 只接受2中规则 "/{controller}"和"/{controller}/{id}"。{controller}可用字符包括:字母,数字,_ * value: 第一项controller类名(文件名除去扩展名必须与类名相同); * 第二项id只能为正整数(包含0) * controller文件必须位于"/controller"文件夹下;类名必须与文件名相同(除去扩展名.php),区分大小写。**/$routes= array( "/resources" => array("resources",""), "/resources/id" => array("resources","id"),);?>

然后route.php解析上面的规则

<?phpclass Route{ private $filepath; private $classname; private $id; private $routepatterns; public function __construct() { $this->filepath = ""; $this->classname = ""; $this->id = null; $this->routepatterns = array(); $this->initRoutes(); } private function initRoutes() { $reg_m1 = "#^/(\w+)$#"; $reg_m2 = "#^/(\w+)/id$#"; $matches = array(); $routes = array(); include "router.php"; foreach($routes as $key=>$value){ if(preg_match($reg_m1,$key,$matches)){ $this->routepatterns[] = array("#^/(".$matches[1].")\??$#i", array("controller/".$value[0].".php",$value[0])); } else if(preg_match($reg_m2,$key,$matches)){ $this->routepatterns[] = array("#^/(".$matches[1].")/(\d+)\??$#i", array("controller/".$value[0].".php",$value[0])); } } } public function processURL($urlpath) { $matches = array(); foreach($this->routepatterns as $router){ if(preg_match($router[0],$urlpath,$matches)){ $filepath_ = "/".$router[1][0]; $classname_ = $router[1][1]; $id_ = count($matches)>2?$matches[2]:null; $this->setFilePath($filepath_); $this->setClassName($classname_); $this->setID($id_); return true; } } return false; } public function setFilePath($filepath) { $this->filepath = $filepath; } public function setClassName($classname) { $this->classname = $classname; } public function setID($id) { $this->id = $id; } public function getFilePath() { return $this->filepath; } public function getClassName() { return $this->classname; } public function getID() { return $this->id; } }?>

<

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。

http://www.pinlue.com/style/images/nopic.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值