> 原文出處: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');
}
}
~~~
希望你能讀懂,沒有的話,關注下一節。