laravel框架呼叫介面實現頁面渲染【小案例】
阿新 • • 發佈:2018-12-21
首先我們先看文件 找到對應的文件
再找到對應的頁面 我要渲染的頁面如下圖 先看看分別都是什麼 比如說 輪播圖 導航 圖片資訊等等...
獲取到了頁面我們寫好路由就開始寫控制器 控制器如下
json_encode跟json_decode這兩個函式 json_oncode是把物件轉化成json格式 而json_decode是轉化成物件或者陣列
控制器 :因為一個頁面需要渲染多個介面 所以要寫成一個方法 別寫成了兩個方法
url是介面地址 file_get_contents獲取檔案 然後轉化成陣列格式然後return返回渲染
public function apicoursel(){ $url=file_get_contents("http://seven.haoyunyun.cn/pictureShow"); $data=json_decode($url,true); $rel=file_get_contents("http://seven.haoyunyun.cn/navRead"); $datas=json_decode($rel,true); $urls=file_get_contents("http://www.laravel55.com/goodsclassindex"); $datab=json_decode($urls,true); $urla=file_get_contents("http://www.laravel55.com/cityshow"); $dataz=json_decode($urla,true); $urlc=file_get_contents("http://www.laravel55.com/article"); $datal=json_decode($urlc,true); return view('apishow/apicoursel',['data'=>$data,'datas'=>$datas,'datab'=>$datab,'dataz'=>$dataz,'datal'=>$datal]); }
如果不確保資料正不正確的可以先列印一下 接下來我們渲染頁面
因為使用了json_decode 而且是true true是陣列 預設false是物件 所以這裡用了中括號
下面是效果