当我们植入状态码后,Msg是不必填的,系统会默认读取/restful/数据格式下标名/msg.php
文件中,状态码对应的default
配置值作为Msg内容。
也可以通过\x\Restful::msg('下标')
方法,指定读取状态码下的哪个msg内容。
以/restful/default/code.php
中的状态码为例,如果开发者是读取里面的SUCCESS
状态码进行输出,
那么框架会默认自动读取:/restful/default/msg.php
中SUCCESS
对应default
的内容设置为Msg值。
如果这时候你在SUCCESS
中新增了一个create_shop_success
的Msg值,在使用时就可以这样用:
\x\Restful::msg('create_shop_success')
依赖类:\x\Restful
方法名:msg()
参数:
int|string $key
返回值:\x\Restful
对象本身
示例代码:
namespace app\http;
use x\controller\Http;
/**
* @Controller(prefix="")
*/
class Index extends Http
{
/**
* @RequestMapping(route="/", method="get", title="主页")
*/
public function index() {
// 使用XML返回结构
// 抛出/restful/default/code.php中的SUCCESS状态码
// 抛出/restful/default/msg.php中的SUCCESS['create_shop_success']提示语
return \x\Restful::type('xml')->code(\x\Restful::SUCCESS())->msg('create_shop_success')->callback();
}
}
用途:自定义MSG内容,一般用于在生命周期、中间件、校验器等全局判断时植入自定义的msg内容。
依赖类:\x\Restful
方法名:setMsg()
(2.5.19+)
参数:
string $msg
返回值:\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::type('xml')->code(\x\Restful::SUCCESS())->setMsg('把我抛出了')->callback();
}
}
注意:setMsg()
的优先级,比msg()
高。