是否可以检测 HTTPRequest 是否可用?
Is it possible to detect if a HTTPRequest is available?
银条纹版本:4.2
我有一个自定义 AssetAdapter
可以根据当前请求对文件系统进行一些更改。我正在使用注入器来获取请求:
$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 和资产的 issue/conflict(GraphQL 似乎并不总是有可用的当前请求)。我想知道是否有办法在尝试通过 Injector
?
获取当前 HTTPRequest 之前检查它是否为 available/setup
是的,如果实例不存在,Injector::inst()->get()
将创建一个新实例。由于 HTTPRequest
在构造过程中需要两个参数,因此您会出错。
您可以使用 ->has()
:
检查是否存在
if (Injector::inst()->has(HTTPRequest::class)) {
$request = Injector::inst()->get(HTTPRequest::class);
// do something
}
银条纹版本:4.2
我有一个自定义 AssetAdapter
可以根据当前请求对文件系统进行一些更改。我正在使用注入器来获取请求:
$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 和资产的 issue/conflict(GraphQL 似乎并不总是有可用的当前请求)。我想知道是否有办法在尝试通过 Injector
?
是的,如果实例不存在,Injector::inst()->get()
将创建一个新实例。由于 HTTPRequest
在构造过程中需要两个参数,因此您会出错。
您可以使用 ->has()
:
if (Injector::inst()->has(HTTPRequest::class)) {
$request = Injector::inst()->get(HTTPRequest::class);
// do something
}