我有一个GET
端点,它接受布尔active
和整数ID
作为查询参数.大概是GET /stuff?active=1&ID=10
吧.
这两个参数都不是必需的.
如果我做GET /stuff?active=1
,并try 通过做$request->get('active')
从Request
中检索值,我会得到一个字符串形式的值1
.
如果启用了declare(strict_types=1)
,并且服务中的方法
public function getBy(bool $active = null) {
...
}
并以Controller
为例调用该方法
$controllerMethod->getBy($request->get('active'))
会得到Argument #0 ($active) must be of type ?bool, string given
,这显然不是很好的结果.
现在我的问题是:Is there a way to cast the request parameters prior to them reaching the 100? Perhaps in a 101 class somehow?
我很清楚我可以在Controller
米中跑$request->has('active') ? $request->get('active') : null
米,但这只是额外的杂乱,我想搬到其他地方go .