基于Hprose for PHP 1.5.6、Yii2.0.7的RPC的基本实现流程
1、已经安装Composer,更新Composer为最新版本,执行命令,结果如图1:
composer self-update
报错:Warning: Module ‘mbstring’ already loaded in Unknown on line 0
2、在php.ini中,发现extension=php_mbstring.dll加载了2次,将1007行注释掉,如图2、3:
;extension=php_mbstring.dll
3、再次运行命令,结果不再报错Warning,但是在https://getcomposer.org/version的文件无法下载,如图4:
composer self-update
4、解决方案,启用Packagist/Composer中国全量镜像,网址:http://pkg.phpcomposer.com/ ,执行命令,如图5:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
报错:
Warning: This development build of composer is over 60 days old. It is recommended to update it by running “C:\ProgramData\ComposerSetup\bin\composer.phar self-update” to get the latest version.
其大意为:
警告:此作曲家的开发版本超过60天。建议通过运行来更新它的”C:\ProgramData\ComposerSetup\bin\composer.phar self-update”,以获取最新版本。
5、再次执行:composer self-update,结果仍然为请求网址:https://getcomposer.org/version ,如图6:
6、打开C:\Users\Administrator\AppData\Roaming\Composer\config.json,清空文件内容,重新执行命令:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
config.json文件内容如图7
{
“repositories”: {
“packagist”: {
“type”: “composer”,
“url”: “https://packagist.phpcomposer.com”
}
}
}
7、再次执行:composer self-update,更新成功,如图8、图9:
Updating to version a8e9df55dc62d8806360be2f79ad112b8678d397.
Downloading: 100%
Use composer self-update –rollback to return to version a066171dcc023ad6429881a7692e46f10e080f99
8、为Composer安装composer asset插件,执行命令,运行结果如图10:
composer global require “fxp/composer-asset-plugin:~1.0.0”
9、执行命令,运行结果如图11:
composer global update
10、安装Yii2,执行命令,运行结果如图12、13:
composer create-project yiisoft/yii2-app-advanced service.ygt.cm
11、查看service.ygt.cm目录,发现已经安装成功,如图14:
12、配置虚拟主机:frontend.service.ygt.cm、backend.service.ygt.cm,打开frontend.service.ygt.cm,报错,如图15
13、原因在于目录E:\wwwroot\service.ygt.cm\frontend\web不存在index.php文件,如图16
14、为Composer更新composer asset插件,执行命令,运行结果如图17:
composer global require “fxp/composer-asset-plugin:~1.1.1”
15、升级Yii2,运行命令,报错,如图18:
composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask
报错:Composer could not find a composer.json file in E:\wwwroot
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ “Getting Started” section
16、进入目录E:\wwwroot\service.ygt.cm,再次执行命令,如图19:
composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask
结果:
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
17、初始化应用,执行命令,如图20:
init
0
yes
18、打开frontend.service.ygt.cm,如图21
19、打开backend.service.ygt.cm,如图22
20、添加应用member.service.ygt.cm,复制目录frontend为目录member,如图23
21、编辑文件E:\wwwroot\service.ygt.cm\environments\index.php,如图24
复制frontend为member
22、将E:\wwwroot\service.ygt.cm\environments下的dev、prod的frontend再次复制为member,如图25
23、执行命令发,如图26:
init
0
yes
All
24、在member目录中搜索frontend,将其替换为member,建议index-test.php文件中的frontend不做替换,如图27
25、编辑E:\wwwroot\service.ygt.cm\common\config\bootstrap.php,如图28
Yii::setAlias(‘@member’, dirname(dirname(__DIR__)) . ‘/member’);
26、打开member.service.ygt.cm,如图29、30
27、安装Hprose for PHP,执行命令,如图31、32:
composer require “hprose/hprose:dev-master”
28、发布远程服务,新建E:\wwwroot\service.ygt.cm\member\controllers\HproseController.php,如图33:
<?php
namespace member\controllers;
use yii\base\Controller;
use Hprose\Yii\Server;
/**
* Hprose controller
*/
class HproseController extends Controller
{
public function beforeAction($action)
{
parent::beforeAction($action);
$server = new Server();
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled(true);
$server->addMethod($action->actionMethod, $this, $action->id);
$server->start();
}
}
或者(建议采用此方案):
namespace member\controllers;
use yii\web\Controller;
use Hprose\Yii\Server;
/**
* Hprose controller
*/
class HproseController extends Controller
{
public $enableCsrfValidation = false;
public function beforeAction($action)
{
parent::beforeAction($action);
$server = new Server();
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled(true);
$server->addMethod($action->actionMethod, $this, $action->id);
$server->start();
}
}
29、服务端实现,新建E:\wwwroot\service.ygt.cm\member\controllers\AvatarController.php,如图34:
<?php
namespace member\controllers;
/**
* Avatar controller
*/
class AvatarController extends HproseController
{
public function actionServer()
{
return 10;
}
public function actionHello() {
return “Hello !”;
}
public function actionSum() {
return strval(array_sum(func_get_args()));
}
}
30、打开网址:http://member.service.ygt.cm/?r=avatar/server ,如图35:
TP3.2.3的HproseController类是将所在控制器的所有方法尽皆发布,而现有实现是只发布当前操作方法
Fa1{s6″server”}z
30、客户端实现,新建E:\wwwroot\service.ygt.cm\zhidao\controllers\SiteController.php,如图36:
<?php
namespace zhidao\controllers;
use yii\web\Controller;
use Hprose\Http\Client;
/**
* Site controller
*/
class SiteController extends Controller
{
public function actionClient() {
$client = new Client(‘http://member.service.ygt.cm/?r=avatar/server’);
echo $client->server();
}
}
31、打开网址:http://zhidao.service.ygt.cm/?r=site/client ,如图37:
32、或者实现hello操作方法,如图38、39:
相对于TP3.2.3的实现,弊端便是即使是远程同一控制器下的操作方法,也是需要实例化两次Client的,不过感觉问题不大,一般来说一个操作方法就足够了。
近期评论