动作、数据表单以及工具表单的响应方法都是同一套方法。
在类中可以通过 $this->response() 获取到 Dcat\Admin\Http\JsonResponse对象并响应数据到前端
return $this->response()->success('成功!');
// 等同于
use Dcat\Admin\Admin;
use Dcat\Admin\Http\JsonResponse;
return JsonResponse::make()->success('成功!');
return Admin::make()->success('成功!');如果是在控制器中使用,需要加上send方法
public function index()
{
return JsonResponse::make()->success('成功!')->send();
}下面介绍一下 JsonResponse 的主要用法
此方法接收一个string类型参数
$this->response()->success('成功!');此方法接收一个string类型参数
$this->response()->error('出错了!');此方法接收一个string类型参数
$this->response()->warning('警告');此方法接收一个string类型参数,可以与success、error、warning等方法同时使用
$this->response()->redirect('auth/users');1秒后自动跳转(非局部刷新),此方法接收一个string类型参数
$this->response()->success('操作成功')->location('auth/users');如果不传参则刷新当前页面
$this->response()->success('操作成功')->location();此方法可以与success、error、warning等方法同时使用
$this->response()->success('xxx')->refresh();此方法接收一个string类型参数
$this->response()->download('auth/users?_export_=1');// 成功
$this->response()->alert(true)->success('...')->detail('详细内容');
// 错误
$this->response()->alert(true)->error('...')->detail('详细内容');
// 警告
$this->response()->alert(true)->warning('...')->detail('详细内容');
// 提示
$this->response()->alert(true)->info('...')->detail('详细内容');此方法可接收一个string、Renderable、Htmlable类型参数,可以与success、error、warning等方法同时使用
响应的
HTML字符默认会被置入动作按钮元素上,如果需要自己控制,则覆写handleHtmlResponse方法即可。
$this->response()->html('<a>a标签</a>');
$this->response()->html(view('...'));此方法接收一个string类型参数,可以与success、error、warning等方法同时使用
$this->response()->script(
<<<JS
console.log('response', response, target);
JS
);上面所有功能接口都支持if模式,如
// 如果 $condition 的值为 真,则调用 refresh 方法
$this->response()->success(...)->ifRefresh($condition);
$this->response()->success(...)->ifLocation($condition, 'auth/users');
// $condition 也可以是闭包
$this->response()->success(...)->ifRefresh(function () {
return true;
});