1.tp5.1的跨域只需要在路由当中加入allowCrossDomain();
Route::resource('api/admin/v1/tags', 'api_admin/Tag')->allowCrossDomain();
2.接口控制器当中
$where = function ($query) use ($request) {
//按商品名称
if ($request->name and $request->name != ' ') {
$search = "%" . $request->name . "%";
$query->where('title', 'like', $search);
}
};
$adverts = Db::table('adverts')->alias('a')
->field('a.*,a.id as aid, b.*')
->join('photo b', 'a.photo_id = b.id')->where($where)
->order('sort_order', 'desc')->paginate(3);
return success_json($adverts, '查询成功!');
助手函数common.php当中
/***
* 定义接口成功消息
* @param $data
* @param string $message
* @return \think\response\Json
*/
function success_json($data, $message = '')
{
$result = [
'ok' => 1,
'data' => $data,
'message' => $message
];
return json($result);
}
/***
* 定义接口失败消息
* @param string $data
* @param $message
* @return \think\response\Json
*/
function error_json($data = '', $message)
{
$result = [
'ok' => 0,
'data' => $data,
'message' => $message
];
return json($result);
}