webman请求的时候一下正常一下报错

xtn

问题描述

同一个接口一下正常一下报错,不知道啥原因
Optional parameter $data declared before required parameter $id is implicitly treated as a required parameter

程序代码

controller代码
    /**
     * 更新數據
     * @param Request $request
     * @param $id
     * @return Response
     * @throws LxBusinessException
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        $res = $this->service->update($data, $id);
        if ($res){
            return api_success('更新成功');
        }
        return api_fail('更新失败');
    }

service代码
 /**
     * 更新信息
     * @param array $data
     * @param int $id
     * @return mixed
     * @throws LxBusinessException
     */
    public function update(array $data = [], int $id)
    {
        if (empty($data) || !$id) {
            throw new LxBusinessException(ApiResponseCode::HTTP_BAD_REQUEST);
        }
        $result = false;
        DB::beginTransaction();
        try {
            $row = $this->model->findOrFail($id);
            // 模型验证
            if ($this->modelValidate) {
                $validate = str_replace("\\model\\", "\\request\\", get_class($this->model));
                if (class_exists($validate)) {
                    //如果开启场景验证
                    if ($this->modelSceneValidate) {
                        \validate($validate)->scene('edit')->check($data);
                    } else {
                        \validate($validate)->check($data);
                    }
                }
            }
            $result = $row->fill($data)->save();
            DB::commit();
        } catch (Throwable $e) {
            Db::rollBack();
            throw new LxBusinessException($e->getMessage());
        }
        return $result;
    }

报错信息

"ErrorException: Optional parameter $data declared before required parameter $id is implicitly treated as a required parameter in D:\\webman_workspace\\lx_cms\\lx-cms-backend\\app\\common\\service\\BaseService.php:212\nStack trace:\n#0 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\composer\\ClassLoader.php(576): support\\App::{closure}()\n#1 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\composer\\ClassLoader.php(576): include()\n#2 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\composer\\ClassLoader.php(427): Composer\\Autoload\\{closure}()\n#3 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\app\\admin\\service\\SystemDeptService.php(8): Composer\\Autoload\\ClassLoader->loadClass()\n#4 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\composer\\ClassLoader.php(576): include('...')\n#5 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\composer\\ClassLoader.php(427): Composer\\Autoload\\{closure}()\n#6 [internal function]: Composer\\Autoload\\ClassLoader->loadClass()\n#7 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Source\\AttributeBasedAutowiring.php(38): class_exists()\n#8 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Source\\AttributeBasedAutowiring.php(63): DI\\Definition\\Source\\AttributeBasedAutowiring->autowire()\n#9 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Source\\SourceChain.php(37): DI\\Definition\\Source\\AttributeBasedAutowiring->getDefinition()\n#10 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Container.php(147): DI\\Definition\\Source\\SourceChain->getDefinition()\n#11 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Container.php(131): DI\\Container->getDefinition()\n#12 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Reference.php(44): DI\\Container->get()\n#13 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Resolver\\ResolverDispatcher.php(55): DI\\Definition\\Reference->resolve()\n#14 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Resolver\\ParameterResolver.php(73): DI\\Definition\\Resolver\\ResolverDispatcher->resolve()\n#15 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Resolver\\ObjectCreator.php(124): DI\\Definition\\Resolver\\ParameterResolver->resolveParameters()\n#16 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Resolver\\ObjectCreator.php(56): DI\\Definition\\Resolver\\ObjectCreator->createInstance()\n#17 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Definition\\Resolver\\ResolverDispatcher.php(60): DI\\Definition\\Resolver\\ObjectCreator->resolve()\n#18 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Container.php(353): DI\\Definition\\Resolver\\ResolverDispatcher->resolve()\n#19 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\php-di\\php-di\\src\\Container.php(184): DI\\Container->resolveDefinition()\n#20 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(316): DI\\Container->make()\n#21 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(351): Webman\\App::Webman\\{closure}()\n#22 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\app\\admin\\middleware\\LxAccessMiddleware.php(44): Webman\\App::Webman\\{closure}()\n#23 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(341): app\\admin\\middleware\\LxAccessMiddleware->process()\n#24 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\webman\\action-hook\\src\\ActionHook.php(15): Webman\\App::Webman\\{closure}()\n#25 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(341): Webman\\ActionHook\\ActionHook->process()\n#26 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(560): Webman\\App::Webman\\{closure}()\n#27 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\App.php(148): Webman\\App::findRoute()\n#28 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\workerman\\Connection\\TcpConnection.php(646): Webman\\App->onMessage()\n#29 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\workerman\\Events\\Select.php(311): Workerman\\Connection\\TcpConnection->baseRead()\n#30 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\workerman\\Worker.php(1485): Workerman\\Events\\Select->loop()\n#31 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\workerman\\Worker.php(1402): Workerman\\Worker::forkWorkersForWindows()\n#32 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\workerman\\Worker.php(560): Workerman\\Worker::forkWorkers()\n#33 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\vendor\\workerman\\webman-framework\\src\\support\\App.php(131): Workerman\\Worker::runAll()\n#34 D:\\webman_workspace\\lx_cms\\lx-cms-backend\\start.php(4): support\\App::run()\n#35 {main}

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

windows系统
"workerman/webman-framework": "^1.5.0",

637 2 0
2个回答

nitron

提示不是写得很清楚么

在强制参数之前指定可选参数都被视为强制参数,即使是使用命名参数调用也是如此

  • xtn 2023-10-08

    大佬好 那我的代码哪里需要修正 麻烦告知下 非常感谢 小的愚昧 没有找到哪里出问题

gddd

array $data = [], int $id 换个位置就行了。可选参数必须放在必须参数的后面

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