请求类型
- 有时,我们需要判断 Request 的请求类型,比如 GET、POST 等等;
2. 可以使用 method()方法来判断当前的请求类型,当然,还有很多专用的请求判断;
3. 使用普通表单提交,通过 method()方法获取类型;<form action="http://localhost/tp5.1/public/rely" method="post">
<input type="text" name="name" value="Lee">
<input type="submit" value="提交">
</form>
return Request::method();
- 在表单提交时,我们也可以设置请求类型伪装,设置隐藏字段_method;
5. 而在判断请求,使用 method(true)可以获取原始请求,否则获取伪装请求;<input type="hidden" name="_method" value="PUT">
Request::method(true);
- 如果想更改请求伪装变量类型的名称,可以在 app.php 中更改;
'var_method' => '_method',
- AJAX/PJAX 伪装,使用?_ajax=1 和?_pjax=1,并使用 isAjax()和 isPjax();
.../rely?_ajax=1
dump(Request::isAjax());
- 这里需要用 isAjax()和 isPjax()来判断,用 method 无法判断是否为 a(p)jax;
9. 在 app.php 也可以更改 ajax 和 pjax 的名称;'var_ajax' => '_ajax',
'var_pjax' => '_pjax',
HTTP 头信息
- 使用 header()方法可以输出 HTTP 头信息,返回是数组类型,也可单信息获取;
dump(Request::header());
dump(Request::header('host'))