Skip to content

Comments

控制器参数绑定支持联合类型和交叉类型#123

Open
luoyue712 wants to merge 4 commits intowalkor:masterfrom
luoyue712:master
Open

控制器参数绑定支持联合类型和交叉类型#123
luoyue712 wants to merge 4 commits intowalkor:masterfrom
luoyue712:master

Conversation

@luoyue712
Copy link
Contributor

@luoyue712 luoyue712 commented Feb 22, 2025

控制器使用联合类型和交叉类型时会出现如下错误:
Error: Call to undefined method ReflectionUnionType::getName() in E:\workerman\webman\vendor\workerman\webman-framework\src\App.php:467

控制器代码:

    public function index(Request|int $request)
    {
        print_r($request);
        return '';
    }

修改后效果:请求参数有request时自动绑定请求参数,否则绑定Request

@luoyue712
Copy link
Contributor Author

luoyue712 commented Feb 24, 2025

实现思路:把原来的逻辑封装为匿名函数,然后根据$parameter->getType()遍历并判断返回类型进行捕获异常。如果返回ReflectionUnionType则在抛出异常是最后一个变量类型进行抛出异常,否则继续遍历。返回ReflectionIntersectionTypeReflectionNamedType时抛出任何异常。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant