【发布时间】:2017-11-15 21:57:40
【问题描述】:
如何通过不重复我的代码使我的控制器返回的数据是 json。
样品控制器
public function getTeams(Request $request){
$result = Team::where('competitionId',$request->input('competitionId',9224))
->orderBy('teamName')
->get(['teamId as id','teamName as name']);
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
}
public function getTeamStats(Request $request) {
if($request->id){
$result = TeamCompetitionStatistics::getTeamStats($request->id);
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
}
}
如你所见,我已将这部分重复了两次
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
他们是不是以更好的方式做到这一点?
【问题讨论】:
标签: php json laravel laravel-5 backend