怎么给request()新增自定义的方法

zhuyujin001

问题描述

这里写问题描述
1、想为\support\Request新增方法isPc()、isCli()、getInfo() ,报错(如下)
2、还有在allRequest.php新增的方法能不能同步到request()里,怎么操作?
我目前如下配置:
在webman/app/AllRequest.php

class AllRequest  extends \support\Request
{
 public function isPc()
 {
   return  ......
 }

 public function isCli()
 {
   return  ......
 }
 public function getInfo()
 {
   return  ......
 }
 ........
}

在config/app.php

use app\AllRequest as Request;
return [
    'debug' => true,
    'error_reporting' => E_ALL,
    'default_timezone' => 'Asia/Shanghai',
    'request_class' => Request::class,
    'public_path' => base_path() . DIRECTORY_SEPARATOR . 'public',
    'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime',
    'controller_suffix' => 'Controller',
    'controller_reuse' => false,
];

在config/dependence.php

return [
    \app\AppRequest::class => function() {
        return request();
    }
];

以上配置后

在项目根目录下的common/Uitls.php里使用

public function ...()
{
(new AllRequest)->isPc()
报错:ArgumentCountError: Too few arguments to function Workerman\Protocols\Http\Request::__construct()
}

还有在allRequest.php新增的方法能不能同步到request()里,怎么操作?

255 1 0
1个回答

walkor

你为什么要new AllRequest?

(new AllRequest)->isPc()

改成

request()->isPc()
  • zhuyujin001 2024-03-19

    哦 我可能是没有重启,开始request()->isPc()是报不存在此方法,重启后又没问题了 感谢啊

  • army 2024-03-21

    这是webman的,workerman的怎么弄?

  • walkor 2024-03-21

    new Worker() 后执行 \Workerman\Protocols\Http::requestClass('自定义Request类名');

🔝