继上篇Laravel RESTful快速部署指南(一) 之后,让我们继续完成一个简单的Laravel RESTful。
开发环境:Windows+XAMPP
代码下载: https://github.com/gmszone/learingphp
编辑器: Sublime Text3(Crack)
转载保留: (转载自Phodal's Blog Phodal's CSDN)
/** * Display the specified resource. * * @param int $id * @return Response */
public function show($id) { //$myid=Athomes::find($id); $maxid=Athomes::where('id','=',$id) ->select('id','temperature','sensors1','sensors2','led1') ->get(); return Response::json($maxid); }用上面的find($id)可以生成对对应id的值。但是有时候我们并不需要所有的数据,比如created_at
{"id":1,"temperature":12,"sensors1":12,"sensors2":23,"led1":0,"created_at":"2013-11-11 08:42:24","updated_at":"2013-11-11 08:42:24"}于是我们就需要用where和select来处理这些数据,使之变成,
{ id: 1, temperature: 12, sensors1: 12, sensors2: 23, led1: 0, created_at: "2013-11-11 08:42:24", updated_at: "2013-11-11 08:42:24" }
$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);这部分参考了来自于这篇博文所写的 http://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers
public function store() { $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); if ($validator->fails()) { return Redirect::to('athome/create') ->withErrors($validator); } 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(); } }
{"temperature":19,"sensors1":22,"sensors2":7.5,"led1":1}程序如下,,网上copy的。
import urllib import urllib2 def post(url, data): req = urllib2.Request(url) data = urllib.urlencode(data) #enable cookie opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) response = opener.open(req, data) return response.read() def main(): posturl = "http://localhost/learingphp/public/athome" data = {"temperature":19,"sensors1":22,"sensors2":7.5,"led1":1} print post(posturl, data) if __name__ == '__main__': main()运行结果如下所示:
public function destroy($id) { // delete $athome = Athomes::find($id); $athome->delete(); }用curl来测试一下,DELETE,那就DELETE id=1吧
curl -X DELETE http://localhost/learingphp/public/athome/1再GET一下,就没有了、
public function update($id) { $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); if ($validator->fails()) { return Redirect::to('athome/' . $id . '/edit') ->withErrors($validator); } else { // store $nerd = Athomes::find($id); $nerd->sensors1 = Input::get('sensors1'); $nerd->sensors2 = Input::get('sensors2'); $nerd->temperature = Input::get('temperature'); $nerd->led1 = Input::get('led1'); $nerd->save(); } }