在做SPA-APP的時候會需要很多api/json來進(jìn)行數(shù)據(jù)通信獲取消息,下面通過一個例子來講解
1、第一步:在控制器文件夾中添加一個controller
<code>php artisan make:controller SearchController</code>
2勺美、第二步:在route.php文件夾中
<code>Route::get('/api/v1/search','SearchController@ searchByName')</code>
3蓝仲、第三步:編輯controller文件
class SearchController extend controller{
private $clientInterface;
public function _constructor(ComicClientInterface $clientInterface){
this->clientInterface=$clientInterface;
}
puclic function searchByName(Request $request){
try{
$name=$request->input('name');
$offest=$request->input('offest');
$result=this->clientInterface->comics($name,$offest);`
return Response::json(['data'=>$results['data'],'message'=>'success getting message'],200);
}catch(\Exception $e){
return Response::json(['data'=[],'message'=>'error getting message'],400)
}
}
}
最后可以得到這樣的內(nèi)容
api.png