Analysis on using phprpc extension mode in Thinkphp framework

Source: Internet
Author: User
Analysis on the application of phprpc extension mode in the Thinkphp framework this project abandoned the original solution of using Axis2 for the web server and switched to phprpc. One is that thinkphp on the server has integrated the interface in this mode, second, the data streams transmitted by phprpc are obviously different from the XML or JSON data volumes in common WebService. analysis on the application of phprpc extension mode in the Thinkphp framework

This project abandoned the original solution of using Axis2 for the web server and switched to phprpc. One is that thinkphp on the server has integrated this mode interface, the second is that the data streams transmitted by phprpc are significantly less than the XML or JSON data volumes in the common WebService, and the data parsing speed is significantly faster due to the relationship between the data volumes ~~

?

To tell the truth, I did not know the phprpc protocol before. I planned to use the restlet form of sina's api for development, but it would take a lot of time to write the database by myself, currently, the lightweight php framework supports only cakephp for rest. for projects that have already been deployed using tp, it is really reluctant. this time, it occasionally lingers on the tp framework official website ~~ Suddenly we found that the 2.1RC version has a new mode-phprpc.

?

It's time to know that phprpc is really good ~~ Like

?

But it seems that this document and case are relatively scarce, and the tp framework does not have the corresponding tutorials and cases to use this mode (official forum does not seem to have a specific use of http://bbs.thinkphp.cn/search.php? Searchid = 156 & orderby = lastpost & ascdesc = desc & searchsubmit = yes )~~ The following are some of the things I wrote after my exploration ~~

?

First understand the phprpc protocol, the specific document (http://www.phprpc.org/zh_CN/docs );

Enter the subject:

?

1. install phprpc

?

?

1. copy the phprpc-mode folder (in AddOns under the official SDK, there is a phprpc folder and phprpc. php file) to the mode folder under the think core file.


2. copy the php SDK of phprpc to the vender folder under the think core file (to be renamed to phprpc)


3. add the following code to the entry file:

?

define('THINK_MODE','PHPRPC'); 

? 4. add the following configuration code to the configuration file:

?

'APP_PHPPRC_ACTIONS'=>'Account,Test,Topic,Timeline,User,Favorites,Follow'

? The string here is the Action to be published as a service, which can be multiple and separated by commas.

?

2. compile Action

?

First, let's take a look at what the phprpc mode tp has done. The following code is taken from app. class. php of phprpc.

?

?

Vendor ('phprpc. phprpc_server '); // instantiate 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 ();

?

? Here, the phprpc core class in the vendor is imported, and each Action requested to be published in the configuration file is traversed so that all public methods are published.

?

Here (http://bbs.thinkphp.cn/viewthread.php? Tid = 21593 & highlight = phprpc) The Forum mentioned that starting a server in Action is actually not feasible. The above is a good explanation. Therefore, all Action methods can be published in phprpc protocol as long as there are parameter input and return parameters.

Instance:

?

Class TestAction extends Action {/*** Test welcome * @ param string $ name */function hello ($ name) {return 'hello'. $ name ;}}

?

?

III. Questions about Model not find

?

If the M () factory method is called in the preceding Action, the Model cannot be found. This problem has taken a long time for me, then I found the solution to phprpc in the mode folder. add the following to the PHP file:

?

THINK_PATH. '/Lib/Think/Core/Model. class. php', // Model class

? This configuration item is also used for the phenomenon that the view model cannot be found. The following is the modified phprpc. php file.

?

Return array (THINK_PATH. '/Common/functions. php', // system function library THINK_PATH. '/Lib/Think/Core/Think. class. php', THINK_PATH. '/Lib/Think/Exception/ThinkException. class. php', // THINK_PATH for exception handling. '/Lib/Think/Core/Log. class. php', // log processing THINK_PATH. '/Mode/Phprpc/App. class. php', // THINK_PATH of the application class. '/Mode/Phprpc/Action. class. php', // controller class THINK_PATH. '/Lib/Think/Core/Model. class. php', // model class THINK_PATH. '/Lib/Think/Core/Model/ViewModel. class. php', // View model class THINK_PATH. '/Mode/Phprpc/alias. php ', // load the alias );

?

IV. Action method return problems

?

Return is used for all return parameters;

Echo can be used to return strings;

If an Exception is returned, throw new Exception ('string', 1) can be thrown directly );

?

?

The steps for using the phprpc mode in the basic Upload tp framework are as follows ~~ This document should be provided for later versions of tp for learning, and we do not need to explore it a little bit. However, we hope tp can develop a restlet mode ~~ Haha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.