(四)——詳解Laravel的RESTful

2018-02-24 15:43 更新

原文出處:https://www.phodal.com/blog/bare-minimum-iot-system-about-restful/

最小物聯網系統(tǒng)(三)——創(chuàng)建RESTful?如果你沒有看懂之前最后的代碼,那么我就簡要的說說:

首頁——index

打開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);
    }

其實原本不需要這么麻煩的,不過這樣寫容易懂。

創(chuàng)建頁——create

http://b.phodal.com/athome/create可以看看這里,一個比較簡單的頁面示例,不過這里用到了模板,我們過后再講講這個模板。

    public function create()
    {
        $maxid=Athomes::max('id');
        return View::make('athome.create')->with('maxid',$maxid);
    }

創(chuàng)建的代碼似乎太簡單了,但是我們忽略了其中 的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);
    }
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號