浅析Thinkphp框架中运用phprpc扩展模式
浅析Thinkphp框架中应用phprpc扩展模式
这次的项目舍弃了原来使用Axis2做web服务端的方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc传输的数据流相对于普通WebService中的XML或JSON形式的数据量明显减少,而且因为数据量的关系解析速度明显比较快~~
?
说实话,以前还真不知道有phprpc这个协议的,本打算使用sina的api的restlet形式开发,但自己写库的话会花比较多的时间,而现在轻量级的php框架支持rest的貌似只有cakephp,对于已经用tp部署的项目,实在是不情愿,这次偶尔在tp框架官网上徘徊一番~~忽然发现2.1RC版本有个新模式――phprpc。
?
用了才知道,phprpc这东西真的不错~~赞一个
?
但貌似这东西文档和案例比较的匮乏,而且tp框架也没有相应的教程和案例来使用这个模式(官方论坛里貌似也没有具体的使用方法http://bbs.thinkphp.cn/search.php?searchid=156&orderby=lastpost&ascdesc=desc&searchsubmit=yes)~~下面是我探索后写下的一些东西~~
?
首先认识下phprpc协议,具体文档(http://www.phprpc.org/zh_CN/docs/);
进入正题:
?
一、安装phprpc模式
?
?
1、将phprpc模式的文件夹(在官方SDK下的AddOns中,有一个phprpc文件夹和phprpc.php文件)拷到think核心文件下的mode文件夹中。
2、将phprpc的php的SDK拷到think核心文件下的Vendor文件夹中(要重命名为phprpc)
3、在入口文件添加如下代码:
?
define('THINK_MODE','PHPRPC');
? 4、在配置文件中添加如下配置代码:
?
'APP_PHPPRC_ACTIONS'=>'Account,Test,Topic,Timeline,User,Favorites,Follow'
?这里的字符串是要发布为服务的Action,可以多个,用逗号隔开
?
二、编写Action
?
首先来看看这边的phprpc模式到底tp做了什么。以下代码摘自phprpc自带的app.class.php
?
?
Vendor('phpRPC.phprpc_server'); //实例化phprpc $server = new PHPRPC_Server(); $actions = explode(',',C('APP_PHPPRC_ACTIONS')); foreach ($actions as $action){ //$server -> setClass($action.'Action'); $temp = $action.'Action'; $methods = get_class_methods($temp); $server->add($methods,new $temp); } $server->setDebugMode(true); $server->setEnableGZIP(true); $server->start();
?
? 这里导入了vendor里的phprpc核心类,再对每个在配置文件里要求发布的Action进行遍历,使所有的public方法全部发布。
?
在这里(http://bbs.thinkphp.cn/viewthread.php?tid=21593&highlight=phprpc)论坛中提到了在Action中start一个服务端,这其实是行不通的。上面的就是很好的解释,所以在所有的Action 的方法中只要有参数传入和参数return便可以以phprpc协议发布。
实例:
?
class TestAction extends Action{ /** * * 测试欢迎 * @param string $name */ function hello($name) { return 'Hello ' . $name; } }
?
?
三、关于Model not find的问题
?
假使在上述Action中调用M()工厂方法,会出现Model找不到的现象,这个问题搞了我很久,后来被我找到了解决方案在mode文件夹下的phprpc.php文件中加入:
?
THINK_PATH.'/Lib/Think/Core/Model.class.php', // 模型类
?这一配置项,对于视图模型找不到的现象的方法也是如此,下面是修改版的phprpc.php文件
?
return array( THINK_PATH.'/Common/functions.php', // 系统函数库 THINK_PATH.'/Lib/Think/Core/Think.class.php', THINK_PATH.'/Lib/Think/Exception/ThinkException.class.php',// 异常处理 THINK_PATH.'/Lib/Think/Core/Log.class.php',// 日志处理 THINK_PATH.'/Mode/Phprpc/App.class.php', // 应用程序类 THINK_PATH.'/Mode/Phprpc/Action.class.php',// 控制器类 THINK_PATH.'/Lib/Think/Core/Model.class.php', // 模型类 THINK_PATH.'/Lib/Think/Core/Model/ViewModel.class.php', // 视图模型类 THINK_PATH.'/Mode/Phprpc/alias.php', // 加载别名 );
?
四、关于Action方法返回问题
?
返回参数统一都是用return;
返回字符串可以用echo;
返回异常可以直接抛出异常throw new Exception('string', 1);
?
?
基本上载tp框架中使用phprpc模式的步骤就是这么几步~~大概等tp的后续版本应该会提供这方面的文档以供学习,也不用我们一点点的摸索了,不过最希望的还是tp能出个restlet模式~~哈哈

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
