Php 是否可以检测到HTTPRequest是否可用?
Silverstripe版本:4.2 我有一个自定义的Php 是否可以检测到HTTPRequest是否可用?,php,silverstripe,silverstripe-4,Php,Silverstripe,Silverstripe 4,Silverstripe版本:4.2 我有一个自定义的AssetAdapter,它根据当前请求对文件系统进行一些更改。我正在使用injector获取请求: $request=Injector::inst()->get(HTTPRequest::class) 在大多数情况下,这很好,但在几个单独的实例中,我得到了错误: ERROR [Emergency]: Uncaught ArgumentCountError: Too few arguments to function SilverStripe
AssetAdapter
,它根据当前请求对文件系统进行一些更改。我正在使用injector获取请求:
$request=Injector::inst()->get(HTTPRequest::class)代码>
在大多数情况下,这很好,但在几个单独的实例中,我得到了错误:
ERROR [Emergency]: Uncaught ArgumentCountError: Too few arguments to function SilverStripe\Control\HTTPRequest::__construct(), 0 passed and at least 2 expected
IN GET /ecms-client/public/markseen
Line 157 in /project/path/vendor/silverstripe/framework/src/Control/HTTPRequest.php
这似乎与GraphQL和资产存在问题/冲突(GraphQL似乎并不总是具有可用的当前请求)。我想知道在尝试通过Injector
获取当前HTTPRequest之前,是否有方法检查当前HTTPRequest是否可用/设置?是的,Injector::inst()->get()
将创建一个新实例,如果还不存在的话。由于HTTPRequest
在构造过程中需要两个参数,因此会出现错误
您可以使用->has()
检查是否存在:
if(Injector::inst()->has(HTTPRequest::class)){
$request=Injector::inst()->get(HTTPRequest::class);
//做点什么
}
哦,我的天哪,这让我头疼不已!这个解决方案非常有效!