没有不值得去解决的问题,也没有不值得去学习的技术!

Basic RPC Implementation Flow With Hprose for PHP 1.5.6 and Yii 2.0.7

Open member.service.ygt.cm

1. Composer is already installed. To update Composer to the latest version, I ran the command, and the result is shown in Figure 1:

composer self-update

Error: Warning: Module ‘mbstring’ already loaded in Unknown on line 0

Error: Warning: Module 'mbstring' already loaded in Unknown on line 0
Error: Warning: Module ‘mbstring’ already loaded in Unknown on line 0

2. In php.ini, I found that extension=php_mbstring.dll was loaded twice. I commented out line 1007, as shown in Figures 2 and 3:

;extension=php_mbstring.dll

In php.ini, I found that extension=php_mbstring.dll was loaded twice. I commented out line 1007.
In php.ini, I found that extension=php_mbstring.dll was loaded twice. I commented out line 1007.
In php.ini, I found that extension=php_mbstring.dll was loaded twice. I commented out line 1007.
In php.ini, I found that extension=php_mbstring.dll was loaded twice. I commented out line 1007.

3. I ran the command again. The Warning no longer appeared, but the file at https://getcomposer.org/version could not be downloaded, as shown in Figure 4:

composer self-update

I ran the command again. The Warning no longer appeared, but the file at https://getcomposer.org/version could not be downloaded.
I ran the command again. The Warning no longer appeared, but the file at https://getcomposer.org/version could not be downloaded.

4. The solution was to enable the Packagist/Composer China full mirror. Website: http://pkg.phpcomposer.com/ . I executed the command, as shown in Figure 5:

composer config -g repo.packagist composer https://packagist.phpcomposer.com

Error:
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.

This means:

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.

Solution: enable the Packagist/Composer China full mirror. Website: http://pkg.phpcomposer.com/
Solution: enable the Packagist/Composer China full mirror. Website: http://pkg.phpcomposer.com/

5. I executed `composer self-update` again, but it still requested the URL: https://getcomposer.org/version, as shown in Figure 6:

I executed `composer self-update` again, but it still requested the URL: https://getcomposer.org/version.
I executed `composer self-update` again, but it still requested the URL: https://getcomposer.org/version.

6. I opened C:\Users\Administrator\AppData\Roaming\Composer\config.json, cleared the file contents, and re-executed the command:
composer config -g repo.packagist composer https://packagist.phpcomposer.com

The contents of the config.json file are shown in Figure 7.

{
“repositories”: {
“packagist”: {
“type”: “composer”,
“url”: “https://packagist.phpcomposer.com”
}
}
}

Opened C:\Users\Administrator\AppData\Roaming\Composer\config.json, cleared the file contents, and re-executed the command.
Opened C:\Users\Administrator\AppData\Roaming\Composer\config.json, cleared the file contents, and re-executed the command.

7. I executed `composer self-update` again, and the update was successful, as shown in Figures 8 and 9:

Updating to version a8e9df55dc62d8806360be2f79ad112b8678d397.
Downloading: 100%
Use composer self-update –rollback to return to version a066171dcc023ad6429881a7692e46f10e080f99

Executed `composer self-update` again, and the update was successful.
Executed `composer self-update` again, and the update was successful.
Executed `composer self-update` again, and the update was successful.
Executed `composer self-update` again, and the update was successful.

8. To install the composer asset plugin for Composer, I executed the command. The result is shown in Figure 10:

composer global require “fxp/composer-asset-plugin:~1.0.0”

Install the composer asset plugin for Composer, execute the command.
Install the composer asset plugin for Composer, execute the command.

9. I executed the command, and the result is shown in Figure 11:

composer global update

composer global update
composer global update

10. To install Yii2, I executed the command. The result is shown in Figures 12 and 13:

composer create-project yiisoft/yii2-app-advanced service.ygt.cm

Install Yii2, execute the command: composer create-project yiisoft/yii2-app-advanced service.ygt.cm
Install Yii2, execute the command: composer create-project yiisoft/yii2-app-advanced service.ygt.cm
Install Yii2, execute the command: composer create-project yiisoft/yii2-app-advanced service.ygt.cm
Install Yii2, execute the command: composer create-project yiisoft/yii2-app-advanced service.ygt.cm

11. I checked the service.ygt.cm directory and found that the installation was successful, as shown in Figure 14:

Checked the service.ygt.cm directory and found that the installation was successful.
Checked the service.ygt.cm directory and found that the installation was successful.

12. I configured the virtual hosts: frontend.service.ygt.cm and backend.service.ygt.cm. Opening frontend.service.ygt.cm resulted in an error, as shown in Figure 15.

Configured virtual hosts: frontend.service.ygt.cm, backend.service.ygt.cm. Opening frontend.service.ygt.cm resulted in an error.
Configured virtual hosts: frontend.service.ygt.cm, backend.service.ygt.cm. Opening frontend.service.ygt.cm resulted in an error.

13. The reason is that the index.php file does not exist in the E:\wwwroot\service.ygt.cm\frontend\web directory, as shown in Figure 16.

The reason is that the index.php file does not exist in the E:\wwwroot\service.ygt.cm\frontend\web directory.
The reason is that the index.php file does not exist in the E:\wwwroot\service.ygt.cm\frontend\web directory.

14. To update the composer asset plugin for Composer, I executed the command. The result is shown in Figure 17:

composer global require “fxp/composer-asset-plugin:~1.1.1”

Update the composer asset plugin for Composer, execute the command.
Update the composer asset plugin for Composer, execute the command.

15. I ran the command to upgrade Yii2, and it produced an error, as shown in Figure 18:
composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask

Error: 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

Upgrade Yii2, run the command, error: Composer could not find a composer.json file.
Upgrade Yii2, run the command, error: Composer could not find a composer.json file.

16. I navigated to the E:\wwwroot\service.ygt.cm directory and executed the command again, as shown in Figure 19:
composer update yiisoft/yii2 yiisoft/yii2-composer bower-asset/jquery.inputmask

Result:
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

Navigated to the E:\wwwroot\service.ygt.cm directory and executed the command again.
Navigated to the E:\wwwroot\service.ygt.cm directory and executed the command again.

17. To initialize the application, I executed the command, as shown in Figure 20:

init

0

yes

Initialize the application, execute the command: init.
Initialize the application, execute the command: init.

18. Open frontend.service.ygt.cm, as shown in Figure 21.

Open frontend.service.ygt.cm
Open frontend.service.ygt.cm

19. Open backend.service.ygt.cm, as shown in Figure 22.

Open backend.service.ygt.cm
Open backend.service.ygt.cm

20. To add the application member.service.ygt.cm, I copied the frontend directory to a directory named member, as shown in Figure 23.

Add the application member.service.ygt.cm, copy the frontend directory to the member directory.
Add the application member.service.ygt.cm, copy the frontend directory to the member directory.

21. Edit the file E:\wwwroot\service.ygt.cm\environments\index.php, as shown in Figure 24.

Copy frontend to member.

Edit the file E:\wwwroot\service.ygt.cm\environments\index.php
Edit the file E:\wwwroot\service.ygt.cm\environments\index.php

22. Under E:\wwwroot\service.ygt.cm\environments, copy the frontend directories in both dev and prod to member as well, as shown in Figure 25.

Under E:\wwwroot\service.ygt.cm\environments, copy the frontend directories in both dev and prod to member as well.
Under E:\wwwroot\service.ygt.cm\environments, copy the frontend directories in both dev and prod to member as well.

23. Execute the command, as shown in Figure 26:
init

0
yes
All

Execute the command
Execute the command

24. Search for frontend in the member directory and replace it with member. It is recommended not to replace frontend in the index-test.php file, as shown in Figure 27.

Search for frontend in the member directory and replace it with member. It is recommended not to replace frontend in the index-test.php file.
Search for frontend in the member directory and replace it with member. It is recommended not to replace frontend in the index-test.php file.

25. Edit E:\wwwroot\service.ygt.cm\common\config\bootstrap.php, as shown in Figure 28.

Yii::setAlias(‘@member’, dirname(dirname(__DIR__)) . ‘/member’);

Yii::setAlias('@member', dirname(dirname(__DIR__)) . '/member');
Yii::setAlias(‘@member’, dirname(dirname(__DIR__)) . ‘/member’);

26. Open member.service.ygt.cm, as shown in Figures 29 and 30.

Open member.service.ygt.cm
Open member.service.ygt.cm
Open member.service.ygt.cm
Open member.service.ygt.cm

27. To install Hprose for PHP, I executed the command, as shown in Figures 31 and 32:

composer require “hprose/hprose:dev-master”

Install Hprose for PHP, execute the command: composer require "hprose/hprose:dev-master"
Install Hprose for PHP, execute the command: composer require “hprose/hprose:dev-master”
Install Hprose for PHP, execute the command: composer require "hprose/hprose:dev-master"
Install Hprose for PHP, execute the command: composer require “hprose/hprose:dev-master”

28. To publish the remote service, I created E:\wwwroot\service.ygt.cm\member\controllers\HproseController.php, as shown in Figure 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();
}
}

Alternatively (this approach is recommended):

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

Publish the remote service, create E:\wwwroot\service.ygt.cm\member\controllers\HproseController.php
Publish the remote service, create E:\wwwroot\service.ygt.cm\member\controllers\HproseController.php

29. For the server-side implementation, I created E:\wwwroot\service.ygt.cm\member\controllers\AvatarController.php, as shown in Figure 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()));
}
}

Server-side implementation, create E:\wwwroot\service.ygt.cm\member\controllers\AvatarController.php
Server-side implementation, create E:\wwwroot\service.ygt.cm\member\controllers\AvatarController.php

30. Open the URL: http://member.service.ygt.cm/?r=avatar/server , as shown in Figure 35:

The HproseController class in TP3.2.3 publishes all methods of the controller it resides in, whereas the current implementation only publishes the current action method.
Fa1{s6″server”}z

The HproseController class in TP3.2.3 publishes all methods of the controller it resides in, whereas the current implementation only publishes the current action method.
The HproseController class in TP3.2.3 publishes all methods of the controller it resides in, whereas the current implementation only publishes the current action method.

30. For the client-side implementation, I created E:\wwwroot\service.ygt.cm\zhidao\controllers\SiteController.php, as shown in Figure 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();
}
}

Client-side implementation, create E:\wwwroot\service.ygt.cm\zhidao\controllers\SiteController.php
Client-side implementation, create E:\wwwroot\service.ygt.cm\zhidao\controllers\SiteController.php

31. Open the URL: http://zhidao.service.ygt.cm/?r=site/client , as shown in Figure 37:

Open the URL: http://zhidao.service.ygt.cm/?r=site/client
Open the URL: http://zhidao.service.ygt.cm/?r=site/client

32. Alternatively, implement the hello action method, as shown in Figures 38 and 39:

Compared to the TP3.2.3 implementation, the drawback is that even for action methods under the same remote controller, the Client needs to be instantiated twice. However, I feel this is not a major issue; generally, one action method is sufficient.

Compared to the TP3.2.3 implementation, the drawback is that even for action methods under the same remote controller, the Client needs to be instantiated twice. However, I feel this is not a major issue; generally, one action method is sufficient.
Compared to the TP3.2.3 implementation, the drawback is that even for action methods under the same remote controller, the Client needs to be instantiated twice. However, I feel this is not a major issue; generally, one action method is sufficient.
Compared to the TP3.2.3 implementation, the drawback is that even for action methods under the same remote controller, the Client needs to be instantiated twice. However, I feel this is not a major issue; generally, one action method is sufficient.
Compared to the TP3.2.3 implementation, the drawback is that even for action methods under the same remote controller, the Client needs to be instantiated twice. However, I feel this is not a major issue; generally, one action method is sufficient.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系