webman构造函数里传request对象,接收请求参数,报错,有遇到的吗

dssxyz

use support\request;
protected $requst = null;

public function __construct(request $request)
{

    $this->request = $request;
}

上面写法会报错
ArgumentCountError: Too few arguments to function app\controller\Login::__construct(), 0 passed in /www/wwwroot/webman/vendor/workerman/webman-framework/src/Container.php on line 30 and exactly 1 expected in /www/wwwroot/webman/app/controller/Login.php:21
2406 2 1
2个回答

2548a

use support\Request;

注意大小写

  • 暂无评论
walkor

webman里的控制器是常驻内存的,当前进程初始化一次后就不会再初始化了,所以构造函数里传递request对象没有意义,因为request对象在以后的请求看来都是过时的数据。如果你需要控制器有个request对象指向当前请求,可以利用beforeAction给请求对象重新赋值。

/**
 * 该方法会在请求前调用 
 */
public function beforeAction(Request $request)
{
    $this->request = $requset;
}

参考 https://www.workerman.net/doc/webman#/controller?id=%e6%8e%a7%e5%88%b6%e5%99%a8%e9%92%a9%e5%ad%90-beforeaction-afteraction

年代过于久远,无法发表回答
🔝