(三)——创建RESTful

最后更新于:2022-04-01 15:43:12

> 原文出处:https://www.phodal.com/blog/bare-minimum-iot-system-create-restful/ 数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。 ~~~ { id: 1, temperature: 14, sensors1: 12, sensors2: 12, led1: 0 } ~~~ ## 数据库迁移 这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。 ### 创建表 表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些将要的因素吧。 ~~~ php artisan migrate:make create_athomes_table ~~~ 打开 app/database/***create_athomes_table.php这里的***是由日期和某些东西组成的,修改生成的代码为下面。 ~~~ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateAthomesTable extends Migration { public function up() { Schema::create('athomes', function(Blueprint $table) { $table->increments('id'); $table->float('temperature'); $table->float('sensors1'); $table->float('sensors2'); $table->boolean('led1'); $table->timestamps(); }); } public function down() { Schema::drop('athomes'); } } ~~~ 意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。 ### 数据库迁移 我们只是写了我们需要的数据的格式而并没有丢到数据库里, ~~~ php artisan migrate ~~~ 这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。 ~~~ mysql -uroot -p use iot; select * from athomes; ~~~ 就可以看到我们写的东西,那么接下来就是创建RESTful 服务了 ## 创建RESTful 用下面的代码实现我们称之为Athomes控制器的创建 ~~~ php artisan controller:make AthomesController ~~~ 就会在app/controllers下面生成下面的代码 ~~~ class AthomesController extends \BaseController { /** * Display a listing of the resource. * * @return Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return Response */ public function create() { // } /** * Store a newly created resource in storage. * * @return Response */ public function store() { // } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } } ~~~ ### Laravel Resources 上面的代码过于沉重,请让我用Ctrl+C来带来点知识吧。。 | Verb | Path | Action | Route Name | | --- | --- | --- | --- | | GET | /resource | index | resource.index | | GET | /resource/create | create | resource.create | | POST | /resource | store | resource.store | | GET | /resource/{resource} | show | resource.show | | GET | /resource/{resource}/edit | edit | resource.edit | | PUT/PATCH | /resource/{resource} | update | resource.update | | DELETE | /resource/{resource} | destroy | resource.destroy | 所以我们只需要专注于创建create,edit,show,destory,等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model加个class ~~~ class Athomes extends Eloquent { protected $table = 'athomes'; } ~~~ 如果你想要的只是控制器Athomes的代码的话。。 ~~~ class AthomesController extends \BaseController { / * Display a listing of the resource. * @return Response / public $restful=true; protected $athome; public function __construct(Athomes $athome) { $this->athome = $athome ; } public function index() { $maxid=Athomes::all(); return Response::json($maxid); } / * Show the form for creating a new resource. * @return Response / public function create() { $maxid=Athomes::max('id'); return View::make('athome.create')->with('maxid',$maxid); } / * Store a newly created resource in storage. * @return Response / 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'); } } / * 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); } / * Show the form for editing the specified resource. * @param int $id * @return Response / 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); } / * Update the specified resource in storage. * @param int $id * @return Response / public function update($id) { // 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/' . $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(); // redirect Session::flash('message', 'Successfully created athome!'); return Redirect::to('athome'); } } / * Remove the specified resource from storage. * @param int $id * @return Response / 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'); } } ~~~ 希望你能读懂,没有的话,关注下一节。
';