使用header()
方法可以设置响应头,该方法从v2.5.23
版本起支持。
当不使用该方法设置$Response->header
时,框架会默认使用/restful/config.php
配置文件中,对应的headers
配置参数。
该方法可用于配置Restful的跨域请求。
依赖类:\x\Restful
方法名:header()
参数:
array $array
返回值:\x\Restful
对象本身
示例代码,设置跨域请求:
namespace app\http;
use x\controller\Http;
/**
* @Controller(prefix="")
*/
class Index extends Http
{
/**
* @RequestMapping(route="/", method="get", title="主页")
*/
public function index() {
return \x\Restful::header([
// 接口跨域设置
'origin' => '*',
// 接口数据请求类型
'type' => '',
// 接口跨域允许请求的类型
'methods' => 'POST,GET,OPTIONS,DELETE',
// 接口是否允许发送 cookies
'credentials' => 'true',
// 接口允许自定义请求头的字段
'headers' => 'Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin, api_key',
])->code(\x\Restful::ERROR())->callback();
}
}