Analysis on the application of PHPRPC expansion model in thinkphp framework

Source: Internet
Author: User
Tags php framework
Analysis on application of PHPRPC extension mode in thinkphp frame

This project abandoned the original use AXIS2 to do Web services end of the scheme, instead of PHPRPC implementation, one is the server thinkphp has integrated the mode interface, The second is that the data flow of the PHPRPC transmission is significantly reduced relative to the XML or JSON form in the normal webservice, and because the relational parsing speed of the data volume is obviously faster ~ ~

?

To tell the truth, I did not know that there is phprpc this agreement, the intention is to use the Sina API Restlet form development, but their own writing library will take more time, and now the lightweight PHP framework support rest of the seemingly only cakephp, for the project has been deployed with TP, is really reluctant, this time occasionally in the TP framework official online ~ ~ Suddenly found that the 2.1RC version has a new mode--phprpc.

?

Used to know, phprpc this thing is really good ~ ~ like a

?

But it seems like this is a lack of documentation and case comparisons, and the TP framework does not have a corresponding tutorial and case to use this model (the official forum does not seem to have specific use methods http://bbs.thinkphp.cn/search.php?searchid=156& Orderby=lastpost&ascdesc=desc&searchsubmit=yes) ~ ~ Here are some things I wrote after I explored ~ ~

?

First recognize the next PHPRPC protocol, specific documents (http://www.phprpc.org/zh_CN/docs/);

Go to the Chase:

?

First, install the PHPRPC mode

?

?

1, the PHPRPC Mode folder (in the official SDK under the addons, there is a Phprpc folder and phprpc.php file) copied to the think core file in the Mode folder.


2. Copy the Phprpc PHP SDK to the vendor folder under the Think core file (to rename to Phprpc)


3. Add the following code to the entry file:

?

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

?

' App_phpprc_actions ' = ' account,test,topic,timeline,user,favorites,follow '

? Here the string is to be published as a service action, can be multiple, separated by commas

?

Second, write action

?

First of all, look at the PHPRPC model here what TP did. The following code is excerpted from the Phprpc app.class.php

?

?

        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 ();

?

? This introduces the Phprpc core class in vendor, and then iterates through each action required to publish in the config file so that all public methods are published.

?

The HTTP://BBS.THINKPHP.CN/VIEWTHREAD.PHP?TID=21593&HIGHLIGHT=PHPRPC forum mentions the start of a server in action, which in fact does not work. The above is a good explanation, so in all action methods, as long as there are parameters and parameter return can be published in the PHPRPC protocol.

Instance:

?

Class Testaction extends action{/** *  * Test Welcome * @param string $name */function Hello ($name) {      return ' hello '. $na Me;  } }

?

?

Third, the problem of model not find

?

If you invoke the M () factory method in the above action, there will be a phenomenon that the model cannot find, and this problem took me a long time and I found the solution. In the phprpc.php file under the Mode folder, add:

?

Think_path. ' /lib/think/core/model.class.php ',//model class

This configuration item is also true for a phenomenon that the view model cannot find, and the following is a modified version of the 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 ',//exception handling    Think_path. ' /lib/think/core/log.class.php ',//log processing    Think_path. ' /mode/phprpc/app.class.php ',//Application class    Think_path. ' /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 aliases);

?

Iv. about the action method return problem

?

Return parameter unification is all with return;

The return string can be used with echo;

The return exception can throw an exception directly to the throw new Exception (' string ', 1);

?

?

Basically, the steps to use the PHPRPC mode in the TP frame are so few steps ~ ~ The subsequent version of TP should provide this document for learning, nor do we have a little bit of groping, but the most hope is TP can be 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.