ThinkChat🤖让你学习和工作更高效,注册即送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); } ~~~