<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > 原文出處:https://www.phodal.com/blog/bare-minimum-iot-system-about-restful/ [最小物聯網系統(三)——創建RESTful](http://www.phodal.com/blog/bare-minimum-iot-system-create-restful/)?如果你沒有看懂之前最后的代碼,那么我就簡要的說說: ## 首頁——index 打開[b.phodal.com](http://b.phodal.com/)我們會看到 ~~~ [{"id":1,"temperature":14,"sensors1":12,"sensors2":12,"led1":1,"created_at":"2013-12-05 12:32:32","updated_at":"2013-12-24 05:50:02"}, {"id":2,"temperature":12,"sensors1":12,"sensors2":12,"led1":1,"created_at":"2013-12-21 16:07:28","updated_at":"2013-12-21 16:07:28"}] ~~~ 這個就是返回效果,我們只需要在上面寫。在AthomesController.php上面的話就是index() 函數里面。 Laravel返回JSON數據是比較簡單的 ~~~ public function index() { $maxid=Athomes::all(); return Response::json($maxid); } ~~~ 其實原本不需要這么麻煩的,不過這樣寫容易懂。 ## 創建頁——create [http://b.phodal.com/athome/create](http://b.phodal.com/athome/create)可以看看這里,一個比較簡單的頁面示例,不過這里用到了模板,我們過后再講講這個模板。 ~~~ public function create() { $maxid=Athomes::max('id'); return View::make('athome.create')->with('maxid',$maxid); } ~~~ 創建的代碼似乎太簡單了,但是我們忽略了其中 的athome.create這個其實是一個模板,位于 ~~~ app/views/athome/create.blade.php ~~~ 這些就有些不好講,當然我們先用簡單的post來做這個,忽略掉這部分吧。 ## 存儲——store 這一部分主要是由create來做的,也就是CRUD中的C ~~~ public function store() { // validate // read more on validation at http://laravel.com/docs/validation $rules = array( 'led1'=>'required', 'sensors1' => 'required|numeric|Min:-50|Max:80', 'sensors2' => 'required|numeric|Min:-50|Max:80', 'temperature' => 'required|numeric|Min:-50|Max:80' ); $validator = Validator::make(Input::all(), $rules); // process the login if ($validator->fails()) { return Redirect::to('athome/create') ->withErrors($validator) ->withInput(Input::except('password')); } else { // store $nerd = new Athomes; $nerd->sensors1 = Input::get('sensors1'); $nerd->sensors2 = Input::get('sensors2'); $nerd->temperature = Input::get('temperature'); $nerd->led1 = Input::get('led1'); $nerd->save(); // redirect Session::flash('message', 'Successfully created athome!'); return Redirect::to('athome'); } } ~~~ 代碼似乎有點長,不過這點代碼也就是先驗證數據再存儲。 以sensors1為例 ~~~ 'sensors1' => 'required|numeric|Min:-50|Max:80', ~~~ 語句的意思是sensors1是必需的,是一個數字,最小-50,最大80,很容易理解吧。接著的 ~~~ $validator = Validator::make(Input::all(), $rules); ~~~ 也就是用于驗證輸入,當驗證成功的時候,進行存儲。 ## 更新——update 大致和上面的create類似,由于不同的是上面的nerd是New,而上面是根據id ~~~ $nerd = Athomes::find($id); ~~~ ## 刪除——destory 如果能理解上面的update下面的delete也能理解了。 ~~~ public function destroy($id) { // delete $athome = Athomes::find($id); $athome->delete(); if(is_null($athome)) { return Response::json('Todo not found', 404); } // redirect Session::flash('message', 'Successfully deleted the nerd!'); return Redirect::to('athome'); } ~~~ ## 編輯——edit 和create一樣用的是模板, ~~~ public function edit($id) { // get the nerd $athome = Athomes::find($id); // show the edit form and pass the nerd return View::make('athome.edit') ->with('athome', $athome); } ~~~ 模板的任務就交給下一篇吧。 ## 展示——show 這個是按照id來展示的, 如 ~~~ athome/1 ~~~ 就是列出這個,返回的也是json數據,為了方便其他,當成一個接口。 ~~~ public function show($id) { $myid=Athomes::find($id); $maxid=Athomes::where('id','=',$id) ->select('id','temperature','sensors1','sensors2','led1') ->get(); return Response::json($maxid); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看