- 调试执行的sql语句
User::get(1);
echo User::getLastSql(); - 方法直接返回当前的查询SQL而不执行fetchSql
echo User::fetchSql()->find(1);
- 手动抛出HTTP异常
throw new \think\exception\HttpException(404, '页面不存在');
或者使用助手函数
abort(404,'页面不存在'); - 输出视图
如果当前控制器继承了Controller类,使用return $this->fetch();
否则使用助手函数 return view() //话说我经常忘了带上return - 调试变量
dump() //用着用着都以为它是php内置函数了,老在其他项目不自觉地就用了
halt() 调试变量输出后中止程序的执行 - 手动抛出异常
// 使用think自带异常类抛出异常
throw new \think\Exception('异常消息', 100006); - 在控制器中指定输出数据类型
namespace app\index\controller;class Index
{
public function index()
{ $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
// 指定json数据输出
return json(['data'=>$data,'code'=>1,'message'=>'操作完成']);
//或者指定 xml数据输出
return xml(['data'=>$data,'code'=>1,'message'=>'操作完成']); }
}