tsingsun / yii2-swoole

make yii2 project runing on swoole
BSD 3-Clause "New" or "Revised" License
162 stars 23 forks source link

yii2最新版,使用这个插件启动后访问出错,提示控制器找不到 #2

Closed neilqin closed 5 years ago

neilqin commented 6 years ago

neil@neil:~/yii2/yii2-app-advanced/frontend/swoole$ sudo /usr/local/php-7.2.3/bin/php http_server_coroutine.php start An Error occurred while handling another error: worker:#0yii\base\InvalidRouteException: Unable to resolve the request "site/error". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537 Stack trace:

0 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/di/ApplicationDecorator.php(41): yii\base\Module->runAction('site/error')

1 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/ErrorHandler.php(108): tsingsun\swoole\di\ApplicationDecorator->__call('runAction', Array)

2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/web/ErrorHandler.php(55): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))

3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(65): tsingsun\swoole\web\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))

4 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

5 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

6 {main}

Previous exception: yii\base\InvalidRouteException: Unable to resolve the request "". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537 Stack trace:

0 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('', Array)

1 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(45): yii\web\Application->handleRequest(Object(tsingsun\swoole\web\Request))

2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

4 {main}

neilqin commented 6 years ago

上面是http://192.168.0.7:9502这样访问的,如果以http://192.168.0.7:9502/index.php?r=index/index来访问一样报错: worker:#0yii\base\InvalidRouteException: Unable to resolve the request "site/error". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537 Stack trace:

0 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/di/ApplicationDecorator.php(41): yii\base\Module->runAction('site/error')

1 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/ErrorHandler.php(108): tsingsun\swoole\di\ApplicationDecorator->__call('runAction', Array)

2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/web/ErrorHandler.php(55): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))

3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(65): tsingsun\swoole\web\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))

4 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

5 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

6 {main}

Previous exception: yii\base\InvalidRouteException: Unable to resolve the request "index/index". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537 Stack trace:

0 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('index/index', Array)

1 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(45): yii\web\Application->handleRequest(Object(tsingsun\swoole\web\Request))

2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

4 {main}

Next yii\web\NotFoundHttpException: Page not found. in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/Application.php:115 Stack trace:

0 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(45): yii\web\Application->handleRequest(Object(tsingsun\swoole\web\Request))

1 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))

tsingsun commented 6 years ago

yii\base\InvalidRouteException: Unable to resolve the request ""

tsingsun commented 6 years ago

是不是默认路由设置问题?

neilqin commented 6 years ago

我用nginx+phpfpm可以正常访问的,直接是官网create-project --prefer-dist yiisoft/yii2-app-advanced 安装的,然后在frontend下面运行了php http_server.php start,

neilqin commented 6 years ago

http_server.php如下:

defined('WEBROOT') or define('WEBROOT', __DIR__);
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
defined('COROUTINE_ENV') or define('COROUTINE_ENV', true);
require(__DIR__ . '/../../vendor/autoload.php');
$config = [
    'class'=>'tsingsun\swoole\server\HttpServer',
    'timeout'=>2,
    'setting' => [
        'daemonize'=>0,
        'max_coro_num'=>300,
        'reactor_num'=>1,
        'worker_num'=>1,
        'pid_file' => __DIR__ . '/../runtime/testHttp.pid',
        'log_file' => __DIR__.'/../runtime/logs/swoole.log',
        'debug_mode'=> 1,
        'user'=>'tsingsun',
        'group'=>'staff',
    ],
];
Server::run($config,function (Server $server){
    $starter = new \tsingsun\swoole\bootstrap\WebApp($server);
    //初始化函数独立,为了在启动时,不会加载Yii相关的文件,在库更新时采用reload平滑启动服务器
    $starter->init = function (\tsingsun\swoole\bootstrap\BaseBootstrap $bootstrap) {
        require(__DIR__ . '/../../vendor/tsingsun/yii2-swoole/src/Yii.php');
        //原项目的配置文件
        $config = \yii\helpers\ArrayHelper::merge(
            require(__DIR__ . '/../config/main.php'),
            require(__DIR__ . '/../config/main-local.php')
        );
        Yii::setAlias('@webroot', WEBROOT);
        Yii::setAlias('@web', '/');
        $bootstrap->appConfig = $config;
    };
    $server->bootstrap = $starter;
    $server->start();
});
tsingsun commented 6 years ago

一般出现第一个问题时,是默认路由.第二个访问,确定"index/index"这个路由没错吗?还有defined('WEBROOT') or define('WEBROOT', DIR);是否正确.webroot指向web目录.

neilqin commented 6 years ago

http://192.168.0.7:9501/index.php?r=site%2Findex 一样的,如下:site/index site/error都找不到,这个是默认安装的,没有修改过程序的

An Error occurred while handling another error:
worker:#0yii\base\InvalidRouteException: Unable to resolve the request "site/error". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537
Stack trace:
#0 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/di/ApplicationDecorator.php(41): yii\base\Module->runAction('site/error')
#1 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/ErrorHandler.php(108): tsingsun\swoole\di\ApplicationDecorator->__call('runAction', Array)
#2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/web/ErrorHandler.php(55): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(65): tsingsun\swoole\web\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#5 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#6 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request "site/index". in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/base/Module.php:537
Stack trace:
#0 /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('site/index', Array)
#1 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(45): yii\web\Application->handleRequest(Object(tsingsun\swoole\web\Request))
#2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#3 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#4 {main}

Next yii\web\NotFoundHttpException: Page not found. in /home/neil/yii2/yii2-app-advanced/vendor/yiisoft/yii2/web/Application.php:115
Stack trace:
#0 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/WebApp.php(45): yii\web\Application->handleRequest(Object(tsingsun\swoole\web\Request))
#1 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/bootstrap/BaseBootstrap.php(124): tsingsun\swoole\bootstrap\WebApp->handleRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#2 /home/neil/yii2/yii2-app-advanced/vendor/tsingsun/yii2-swoole/src/server/HttpServer.php(40): tsingsun\swoole\bootstrap\BaseBootstrap->onRequest(Object(Swoole\Http\Request), Object(Swoole\Http\Response))
#3 {main}
neilqin commented 6 years ago

defined('WEBROOT') or define('WEBROOT', DIR); 这个我目前把http_server.php直接放在frontend的web下面了,使用__DIR___应该不会有问题的,yii2版本用的是2.0.16

tsingsun commented 6 years ago

anvance项目有额外的配置,,比如 需要在启动脚本加入通用配置. require DIR . '/../../common/config/bootstrap.php'; 还有一些别名需要设置,还请参照advance项目配置相应调整.

neilqin commented 6 years ago

这么说我都不知道怎么改了,nginx+fpm是可以正常访问的,说明项目配置没有问题,从启动脚本来看,应该也是基于advance项目的吧, require(DIR . '/../config/main.php'), require(DIR . '/../config/main-local.php'),

不知道怎么改了

tsingsun commented 6 years ago

可以这种说,原index.php是php-fpm的启动脚本,现在http_server.php是swoole的启动脚本,.要把index.php的配置代码移到http_server..$starter->init 这个callback里,就是来干这事的.