美文网首页程序员湿货
搭建REST风格的服务器

搭建REST风格的服务器

作者: 昱全yuquan | 来源:发表于2015-02-04 21:22 被阅读584次

今天购入阿里云,然后准备在阿里云上搭建rest风格的API,首先就是URL规则重写

1、URL重写

在apache服务器下,修改httpd.conf文件

(1)Options FollowSymLinks

AllowOverride None

改为

Options FollowSymLinks

AllowOverride All

(2)去掉下面的注释

LoadModule rewrite_module modules/mod_rewrite.so

2、根目录下添加.htaccess文件,完成重写规则

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

</IfModule>

3、实现index.php

首先是define相关的文件夹路径,比如:

//根路径

define('ROOT_PATH',dirname(__FILE__));

define('CONF_PATH',ROOT_PATH.'/conf/');

然后调用dispatch做分发处理

4、实现dispatch.php

通过获取请求参数来分发到不同的文件和方法中

public function dealUrl(){

$url =strip_tags( $_SERVER['REQUEST_URI'] );

$pathseg =explode('/',trim($url,'/'));

$method = Http_Request::get('method');

if(is_null($method)){

throw new Exception('not_found');

}

$resseg =explode('?',trim($pathseg[1],'?'));

$args[] = $resseg[0];

$strFileName =CONTROLLER_PATH.$resseg[0]."Controller.php";

$this->_controllerFile=$resseg[0]."Controller.php";

$this->_strControllerName= $resseg[0];

if(is_file($strFileName)) {

include_once'Controller.php';

include_once$strFileName;

if(!class_exists($this->_strControllerName)) {

throw newException('not_found');

return'false';

}

$objController =new$this->_strControllerName();

if(!method_exists($objController,$method)){

throw newException('not_found');

}

$objController->$method();

}

}

大工告成!

相关文章

  • 搭建REST风格的服务器

    今天购入阿里云,然后准备在阿里云上搭建rest风格的API,首先就是URL规则重写 1、URL重写 在apache...

  • Rest 风格

    参考:知乎理解本真的REST架构风格REST架构风格最重要的架构约束有6个: 客户-服务器(Client-Serv...

  • Getting Started with Node, Expre

    入门:学习用Express, Postgress 和Sequelize搭建简单的REST服务器 Getting S...

  • 反向代理、负载均衡!优秀的 Nginx 是如何做到的?

    Nginx 的产生 Nginx 同 Apache 一样都是一种 Web 服务器。基于 REST 架构风格,以统一资...

  • REST之前:Programmable Web

    REST架构风格的服务(或者它开放的api),属于Programmable Web。研究REST 架构风格,要从P...

  • REST风格

    一、什么是REST REST是一种软件架构风格,或者说是一种规范,其强调HTTP应当以资源为中心,并且规范了URI...

  • REST风格

    什么是REST风格 REST(Representational State Transfer表述性状态转移)是一种...

  • SpringBoot HATEOAS用法简介

    REST风格简介 介绍HATEOAS之前先简单介绍一下REST,REST 是 Representational s...

  • RESTful API知识整理

    不是标准,是设计风格 REST(英文:Representational State Transfer,简称REST...

  • nginx

    Nginx同 Apache 一样都是一种 Web 服务器。基于 REST 架构风格,以统一资源描述符(Unifor...

网友评论

    本文标题:搭建REST风格的服务器

    本文链接:https://www.haomeiwen.com/subject/yrprxttx.html