您好,欢迎访问一九零五行业门户网

PHP REST架构简略设计

php rest架构简单设计
rest是什么?rest(representational state transfer表述性状态转移)是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。
rest的特点:
网络上的所有事物都被抽象为资源(resource)每个资源对应一个唯一的资源标识(resource identifier)通过通用的连接器接口(generic connector interface)对资源进行操作;对资源的各种操作不会改变资源标识;所有的操作都是无状态的(stateless)。rest的通俗说明:
rest是通过http请求的状态,去服务器端请求不同的服务方法,但是请求地址是相同的。比如请求一个http://xxxxx.com/user/的接口为例例如post方法,一般提供数据新增的功能,如果客户端是post方法,请求上面的接口,那就是告诉服务器,是新增的操作。例如get方法,一般是获取数据,因为get方法是默认的方法,不会对数据造成改变,所以一般是获取数据,通过get方法请求上面的接口,就是获取用户数据例如put方法,一般是更新操作,告诉服务器,更新用户信息。例如del方法,一般是删除操作。通过http的4中请求方法,就可以在同一个接口地址上产生四种不同的请求,对接口的扩展性提供了巨大的帮助。rest php端代码简单实现:/** * demo * @author zhuli */class indexcontroller extends controller { public $initphp_list = array('test', 'user'); //action白名单 public $isrest = array('user'); //请求index.php?c=index&a=user接口,通过curl的不同请求状态 public function run() { $curl = $this->getlibrary('curl'); $a = $curl->put('http://10.9.11.1/initphp_32/demo/www/?c=index&a=user', array('username' => 'hello')); print_r($a); } //当请求方式是get方法的时候 public function user_get() { echo 'get'; } //post方法 public function user_post() { $username = $this->controller->get_gp('username', 'p'); echo $username; echo 'get'; } //put public function user_put() { $username = $this->controller->get_gp('username', 'u'); echo $username; echo 'put'; } //del public function user_del() { $username = $this->controller->get_gp('username', 'd'); echo $username; echo 'del'; }}
参考:百度,initphp框架rest实现
其它类似信息

推荐信息