Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以检测到HTTPRequest是否可用?_Php_Silverstripe_Silverstripe 4 - Fatal编程技术网

Php 是否可以检测到HTTPRequest是否可用?

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

Silverstripe版本:4.2

我有一个自定义的
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);
//做点什么
}

哦,我的天哪,这让我头疼不已!这个解决方案非常有效!