原文出處:https://www.phodal.com/blog/bare-minimum-iot-system-about-restful/
最小物聯(lián)網(wǎng)系統(tǒng)(三)——?jiǎng)?chuàng)建RESTful?如果你沒(méi)有看懂之前最后的代碼,那么我就簡(jiǎn)要的說(shuō)說(shuō):
打開(kāi)b.phodal.com我們會(huì)看到
[{"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"}]
這個(gè)就是返回效果,我們只需要在上面寫(xiě)。在AthomesController.php上面的話就是index() 函數(shù)里面。 Laravel返回JSON數(shù)據(jù)是比較簡(jiǎn)單的
public function index()
{
$maxid=Athomes::all();
return Response::json($maxid);
}
其實(shí)原本不需要這么麻煩的,不過(guò)這樣寫(xiě)容易懂。
http://b.phodal.com/athome/create可以看看這里,一個(gè)比較簡(jiǎn)單的頁(yè)面示例,不過(guò)這里用到了模板,我們過(guò)后再講講這個(gè)模板。
public function create()
{
$maxid=Athomes::max('id');
return View::make('athome.create')->with('maxid',$maxid);
}
創(chuàng)建的代碼似乎太簡(jiǎn)單了,但是我們忽略了其中 的athome.create這個(gè)其實(shí)是一個(gè)模板,位于
app/views/athome/create.blade.php
這些就有些不好講,當(dāng)然我們先用簡(jiǎn)單的post來(lái)做這個(gè),忽略掉這部分吧。
這一部分主要是由create來(lái)做的,也就是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');
}
}
代碼似乎有點(diǎn)長(zhǎng),不過(guò)這點(diǎn)代碼也就是先驗(yàn)證數(shù)據(jù)再存儲(chǔ)。 以sensors1為例
'sensors1' => 'required|numeric|Min:-50|Max:80',
語(yǔ)句的意思是sensors1是必需的,是一個(gè)數(shù)字,最小-50,最大80,很容易理解吧。接著的
$validator = Validator::make(Input::all(), $rules);
也就是用于驗(yàn)證輸入,當(dāng)驗(yàn)證成功的時(shí)候,進(jìn)行存儲(chǔ)。
大致和上面的create類(lèi)似,由于不同的是上面的nerd是New,而上面是根據(jù)id
$nerd = Athomes::find($id);
如果能理解上面的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');
}
和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);
}
模板的任務(wù)就交給下一篇吧。
這個(gè)是按照id來(lái)展示的, 如
athome/1
就是列出這個(gè),返回的也是json數(shù)據(jù),為了方便其他,當(dāng)成一個(gè)接口。
public function show($id)
{
$myid=Athomes::find($id);
$maxid=Athomes::where('id','=',$id)
->select('id','temperature','sensors1','sensors2','led1')
->get();
return Response::json($maxid);
}
更多建議: