【发布时间】:2020-09-24 10:12:53
【问题描述】:
感谢 Symfony HttpFoundation 组件,我们可以像以下脚本一样检索 服务器参数:
// retrieves SERVER variables
$request->server->get('HTTP_HOST')
所以,我有以下构造,我想要服务器参数:
public function __construct(RequestStack $requestStack)
{
$request = $requestStack->getCurrentRequest();
$this->country = self::DEFAULT_COUNTRY;
$this->lang = self::DEFAULT_LANG;
$this->brand = self::DEFAULT_BRAND;
$this->jobBrand = $this->brand;
if ($request) {
if (!empty($request->server->get(self::ENV_COUNTRY_CODE))) {
$this->country = $request->server->get(self::ENV_COUNTRY_CODE);
}
if (!empty($request->server->get(self::ENV_LANG_CODE))) {
$this->lang = $request->server->get(self::ENV_LANG_CODE);
}
if (!empty($request->server->get(self::ENV_BRAND))) {
$this->jobBrand = $request->server->get(self::ENV_BRAND);
$this->brand = str_replace('pro', '', $this->jobBrand);
}
if (empty($this->country) || empty($this->lang)) {
throw new NoApacheLocaleException();
}
}
}
有关信息,在测试阶段,我使用 Postman 作为 http 客户端。
所以我的问题是:如何通过 Postman 发送我的参数以便通过 $request->server->get('param') 获取它?
【问题讨论】:
-
这取决于您是否请求 GET、POST、PUT 动词。
-
@HéctorPrats 我用的是 POST 方法
-
简短的回答是:你不能。 $_SERVER 由 PHP 根据服务器端信息进行初始化。不使用来自 HTTP 请求的任何内容。我想你可以通过使用像 _server_http_host 这样的变量名来伪造它进行测试,并相应地调整你的代码。但是如果你使用的是 Symfony 框架,那么你最好使用 Symfony 的功能测试方法。
-
@Cerad 我认为你是对的。
-
@Cerad 你可以把答案写出来,我想验证一下,每个人都可以使用
标签: symfony postman symfony4 symfony-3.4