webman在php8自动注入initialization报错

dsx5ds

问题描述

已根据webman文档和php-di 7.0都做了修改
通过路由定义

Route::any('/test',[app\controller\Index\IndexController::class, 'index']);

访问/test正常可以注入service,但是如果是闭包路由则报错无法初始化service

猜测原因可能是index入口是从route的闭包中直接输出,而不是走正常的流程导致bug,请各位知道

程序代码

#config/container.php
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
$builder->useAttributes(true);
return $builder->build();

#IndexController.php
use DI\Attribute\Inject;
use app\service\CService;

class IndexController extends BaseController
{
    #[Inject]
    private CService $service;

    public function index(Request $request)
    {
      return $this->service->getList();
    }
}

报错信息

Error: Typed property app\controller\Index\IndexController::$service must not be accessed before initialization in /data/webman/app/controller/Index/IndexController.php:26
Stack trace:
#0 /data/webman/config/route.php(54): app\controller\Index\IndexController->index()
#1 /data/webman/vendor/workerman/webman-framework/src/App.php(388): Webman\Route::{closure}()
#2 /data/webman/vendor/workerman/webman-framework/src/App.php(349): Webman\App::Webman\{closure}()
#3 /data/webman/app/middleware/Auth.php(33): Webman\App::Webman\{closure}()
#4 /data/webman/vendor/workerman/webman-framework/src/App.php(341): app\middleware\Auth->process()
#5 /data/webman/vendor/workerman/webman-framework/src/App.php(560): Webman\App::Webman\{closure}()
#6 /data/webman/vendor/workerman/webman-framework/src/App.php(148): Webman\App::findRoute()
#7 /data/webman/vendor/workerman/workerman/Connection/TcpConnection.php(646): Webman\App->onMessage()
#8 /data/webman/vendor/workerman/workerman/Events/Select.php(311): Workerman\Connection\TcpConnection->baseRead()
#9 /data/webman/vendor/workerman/workerman/Worker.php(1638): Workerman\Events\Select->loop()
#10 /data/webman/vendor/workerman/workerman/Worker.php(1429): Workerman\Worker::forkOneWorkerForLinux()
#11 /data/webman/vendor/workerman/workerman/Worker.php(1403): Workerman\Worker::forkWorkersForLinux()
#12 /data/webman/vendor/workerman/workerman/Worker.php(1788): Workerman\Worker::forkWorkers()
#13 /data/webman/vendor/workerman/workerman/Worker.php(1724): Workerman\Worker::monitorWorkersForLinux()
#14 /data/webman/vendor/workerman/workerman/Worker.php(562): Workerman\Worker::monitorWorkers()
#15 /data/webman/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()
#16 /data/webman/start.php(4): support\App::run()
#17 {main}

操作系统及workerman/webman等框架组件具体版本

"require": {
"php": ">=7.2",
"workerman/webman-framework": "^1.5.0",
"monolog/monolog": "^2.0",
"webman/admin": "^0.6.24",
"illuminate/redis": "^9.52",
"illuminate/events": "^9.52",
"webman/think-cache": "^1.0",
"topthink/think-template": "^3.0",
"psr/container": "^2.0",
"php-di/php-di": "7.0.0-rc1",
"doctrine/annotations": "^2.0",
"vlucas/phpdotenv": "^5.5",
"illuminate/pagination": "^9.52"
},

704 1 0
1个回答

walkor 打赏

贴闭包路由代码

  • dsx5ds 2024-03-12

    已解决 看你的帖子控制器需要用Container而不是new

×
🔝