如何解决通过邮递员发送“服务器参数”:
由于 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客户端。
所以我的问题是:如何通过邮递员发送参数,以便通过 $ request-> server-> get('param') 来获取它?
解决方法
根据@Cerad,我对他的回答深信不疑:
$ _ SERVER 由PHP根据服务器端信息初始化。不使用HTTP请求中的任何内容。我想您可以使用变量名称(例如 _server_http_host )来伪造它以进行测试,并相应地调整代码。但是,如果您使用的是Symfony框架,那么使用Symfony的功能测试方法可能会更好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。