浅析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模式~~哈哈

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.