默认的情况下发送的http状态码是200,如果需要返回其它的状态码,可以使用:
~~~
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = ['name' => 'thinkphp', 'status' => '1'];
return json($data, 201);
}
}
~~~
或者发送更多的响应头信息:
~~~
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = ['name' => 'thinkphp', 'status' => '1'];
return json($data, 201, ['Cache-control' => 'no-cache,must-revalidate']);
}
}
~~~
也支持使用下面的链式调用的方式:
~~~
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = ['name' => 'thinkphp', 'status' => '1'];
return json($data)->code(201)->header(['Cache-control' => 'no-cache,must-revalidate']);
}
}
~~~