|
@@ -59,58 +59,75 @@ ps: 在下文中我会继续用MySQL,而不是MariaDB,MairaDB是MySQL的一
|
|
|
|
|
|
Ubuntu/Debian/Mint
|
|
|
|
|
|
- $ sudo apt-get install mariadb-server
|
|
|
+```bash
|
|
|
+$ sudo apt-get install mariadb-server
|
|
|
+```
|
|
|
|
|
|
Fedora/Centos
|
|
|
|
|
|
- $ sudo yum install mariadb-server
|
|
|
+```bash
|
|
|
+$ sudo yum install mariadb-server
|
|
|
+```
|
|
|
|
|
|
openSUSE
|
|
|
|
|
|
- $ sudo zypper install mariadb-server
|
|
|
+```bash
|
|
|
+$ sudo zypper install mariadb-server
|
|
|
+```
|
|
|
|
|
|
Mac OS
|
|
|
-
|
|
|
- $ brew install mariadb
|
|
|
+
|
|
|
+```bash
|
|
|
+$ brew install mariadb
|
|
|
+```
|
|
|
|
|
|
###配置MySQL
|
|
|
|
|
|
修改database.php
|
|
|
|
|
|
- app/config/database.php
|
|
|
+```bash
|
|
|
+app/config/database.php
|
|
|
+```
|
|
|
|
|
|
要修改的就是这个
|
|
|
|
|
|
- 'mysql' => array(
|
|
|
- 'driver' => 'mysql',
|
|
|
- 'host' => 'localhost',
|
|
|
- 'database' => 'iot',
|
|
|
- 'username' => 'root',
|
|
|
- 'password' => '940217',
|
|
|
- 'charset' => 'utf8',
|
|
|
- 'collation' => 'utf8_unicode_ci',
|
|
|
- 'prefix' => '',
|
|
|
- ),
|
|
|
+```php
|
|
|
+'mysql' => array(
|
|
|
+ 'driver' => 'mysql',
|
|
|
+ 'host' => 'localhost',
|
|
|
+ 'database' => 'iot',
|
|
|
+ 'username' => 'root',
|
|
|
+ 'password' => '940217',
|
|
|
+ 'charset' => 'utf8',
|
|
|
+ 'collation' => 'utf8_unicode_ci',
|
|
|
+ 'prefix' => '',
|
|
|
+),
|
|
|
+```
|
|
|
|
|
|
如果你已经有phpmyadmin,似乎对你来说已经很简单了,如果没有的话,就直接用
|
|
|
|
|
|
- $ mysql -uroot -p
|
|
|
-
|
|
|
+```bash
|
|
|
+$ mysql -uroot -p
|
|
|
+```
|
|
|
来创建一个新的
|
|
|
|
|
|
- CREATE DATABASE IF NOT EXISTS iot default charset utf8 COLLATE utf8_general_ci;
|
|
|
+```sql
|
|
|
+CREATE DATABASE IF NOT EXISTS iot default charset utf8 COLLATE utf8_general_ci;
|
|
|
+```
|
|
|
|
|
|
[composer]: https://getcomposer.org/Composer-Setup.exe
|
|
|
|
|
|
数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。
|
|
|
|
|
|
- {
|
|
|
- "id": 1,
|
|
|
- "temperature": 14,
|
|
|
- "sensors1": 12,
|
|
|
- "sensors2": 12,
|
|
|
- "led1": 0
|
|
|
- }
|
|
|
+```javascript
|
|
|
+{
|
|
|
+"id": 1,
|
|
|
+"temperature": 14,
|
|
|
+"sensors1": 12,
|
|
|
+"sensors2": 12,
|
|
|
+"led1": 0
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
##数据库迁移
|
|
|
|
|
@@ -121,34 +138,38 @@ Mac OS
|
|
|
表的概念,类似于在Excel中的表,如果你真实不懂数据库。
|
|
|
让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。
|
|
|
|
|
|
- $ php artisan migrate:make create_athomes_table
|
|
|
+```bash
|
|
|
+$ php artisan migrate:make create_athomes_table
|
|
|
+```
|
|
|
|
|
|
打开 app/database/migrations/***create_athomes_table.php这里的***是由日期和某些东西组成的,修改生成的代码为下面。
|
|
|
|
|
|
- use Illuminate\Database\Schema\Blueprint;
|
|
|
- use Illuminate\Database\Migrations\Migration;
|
|
|
+```php
|
|
|
+use Illuminate\Database\Schema\Blueprint;
|
|
|
+use Illuminate\Database\Migrations\Migration;
|
|
|
|
|
|
- class CreateAthomesTable extends 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()
|
|
|
+ public function up()
|
|
|
+ {
|
|
|
+ Schema::create('athomes', function(Blueprint $table)
|
|
|
{
|
|
|
- Schema::drop('athomes');
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ $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。
|
|
@@ -157,14 +178,20 @@ Mac OS
|
|
|
|
|
|
我们只是写了我们需要的数据的格式而并没有丢到数据库里,
|
|
|
|
|
|
- $ php artisan migrate
|
|
|
+```bash
|
|
|
+$ php artisan migrate
|
|
|
+```
|
|
|
|
|
|
这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。
|
|
|
|
|
|
+```bash
|
|
|
$ mysql -uroot -p
|
|
|
+```
|
|
|
|
|
|
- use iot;
|
|
|
- select * from athomes;
|
|
|
+```sql
|
|
|
+use iot;
|
|
|
+select * from athomes;
|
|
|
+```
|
|
|
|
|
|
就可以看到我们写的东西,那么接下来就是创建RESTful服务了
|
|
|
|
|
@@ -173,41 +200,45 @@ Mac OS
|
|
|
|
|
|
用下面的代码实现我们称之为Athomes控制器的创建
|
|
|
|
|
|
- $ php artisan controller:make AthomesController
|
|
|
+```bash
|
|
|
+$ php artisan controller:make AthomesController
|
|
|
+```
|
|
|
|
|
|
就会在app/controllers下面生成下面的代码
|
|
|
|
|
|
- class AthomesController extends \BaseController {
|
|
|
+```php
|
|
|
+class AthomesController extends \BaseController {
|
|
|
|
|
|
- public function index()
|
|
|
- {
|
|
|
- }
|
|
|
+ public function index()
|
|
|
+ {
|
|
|
+ }
|
|
|
|
|
|
- public function create()
|
|
|
- {
|
|
|
- }
|
|
|
+ public function create()
|
|
|
+ {
|
|
|
+ }
|
|
|
|
|
|
- public function store()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public function show($id)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public function edit($id)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public function update($id)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public function destroy($id)
|
|
|
- {
|
|
|
- }
|
|
|
+ public function store()
|
|
|
+ {
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ public function show($id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public function edit($id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update($id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public function destroy($id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
##Laravel Resources
|
|
|
|
|
@@ -226,148 +257,151 @@ DELETE | /resource/{resource} | destroy | resource.destroy
|
|
|
所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在
|
|
|
app/model 加个 class
|
|
|
|
|
|
- class Athomes extends Eloquent {
|
|
|
- protected $table = 'athomes';
|
|
|
- }
|
|
|
+```php
|
|
|
+class Athomes extends Eloquent {
|
|
|
+ protected $table = 'athomes';
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
如果你想要的只是控制器Athomes的代码的话。。
|
|
|
|
|
|
+```php
|
|
|
+class AthomesController extends \BaseController {
|
|
|
|
|
|
- class AthomesController extends \BaseController {
|
|
|
-
|
|
|
- public $restful=true;
|
|
|
-
|
|
|
- protected $athome;
|
|
|
-
|
|
|
- public function __construct(Athomes $athome)
|
|
|
- {
|
|
|
- $this--->athome = $athome ;
|
|
|
- }
|
|
|
-
|
|
|
- public function index()
|
|
|
- {
|
|
|
- $maxid=Athomes::all();
|
|
|
- return Response::json($maxid);
|
|
|
- }
|
|
|
-
|
|
|
- public function create()
|
|
|
- {
|
|
|
- $maxid=Athomes::max('id');
|
|
|
- return View::make('athome.create')->with('maxid',$maxid);
|
|
|
- }
|
|
|
-
|
|
|
- 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)
|
|
|
- ->withInput(Input::except('password'));
|
|
|
- } else {
|
|
|
- $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();
|
|
|
-
|
|
|
- Session::flash('message', 'Successfully created athome!');
|
|
|
- return Redirect::to('athome');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public function show($id)
|
|
|
- {
|
|
|
- $myid=Athomes::find($id);
|
|
|
- $maxid=Athomes::where('id','=',$id)
|
|
|
- ->select('id','temperature','sensors1','sensors2','led1')
|
|
|
- ->get();
|
|
|
- return Response::json($maxid);
|
|
|
- }
|
|
|
-
|
|
|
- public function edit($id)
|
|
|
- {
|
|
|
- $athome = Athomes::find($id);
|
|
|
-
|
|
|
- return View::make('athome.edit')
|
|
|
- ->with('athome', $athome);
|
|
|
- }
|
|
|
-
|
|
|
- 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 {
|
|
|
- $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();
|
|
|
-
|
|
|
- Session::flash('message', 'Successfully created athome!');
|
|
|
- return Redirect::to('athome');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public function destroy($id)
|
|
|
- {
|
|
|
-
|
|
|
- $athome = Athomes::find($id);
|
|
|
- $athome->delete();
|
|
|
- if(is_null($athome))
|
|
|
- {
|
|
|
- return Response::json('Todo not found', 404);
|
|
|
- }
|
|
|
-
|
|
|
- Session::flash('message', 'Successfully deleted the nerd!');
|
|
|
- return Redirect::to('athome');
|
|
|
- }
|
|
|
+ public $restful=true;
|
|
|
|
|
|
- }
|
|
|
+ protected $athome;
|
|
|
|
|
|
-希望你能读懂,没有的话,关注下一节。
|
|
|
+ public function __construct(Athomes $athome)
|
|
|
+ {
|
|
|
+ $this--->athome = $athome ;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function index()
|
|
|
+ {
|
|
|
+ $maxid=Athomes::all();
|
|
|
+ return Response::json($maxid);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function create()
|
|
|
+ {
|
|
|
+ $maxid=Athomes::max('id');
|
|
|
+ return View::make('athome.create')->with('maxid',$maxid);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ ->withInput(Input::except('password'));
|
|
|
+ } else {
|
|
|
+ $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();
|
|
|
+
|
|
|
+ Session::flash('message', 'Successfully created athome!');
|
|
|
+ return Redirect::to('athome');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function show($id)
|
|
|
+ {
|
|
|
+ $myid=Athomes::find($id);
|
|
|
+ $maxid=Athomes::where('id','=',$id)
|
|
|
+ ->select('id','temperature','sensors1','sensors2','led1')
|
|
|
+ ->get();
|
|
|
+ return Response::json($maxid);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function edit($id)
|
|
|
+ {
|
|
|
+ $athome = Athomes::find($id);
|
|
|
+
|
|
|
+ return View::make('athome.edit')
|
|
|
+ ->with('athome', $athome);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ $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();
|
|
|
+
|
|
|
+ Session::flash('message', 'Successfully created athome!');
|
|
|
+ return Redirect::to('athome');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function destroy($id)
|
|
|
+ {
|
|
|
+
|
|
|
+ $athome = Athomes::find($id);
|
|
|
+ $athome->delete();
|
|
|
+ if(is_null($athome))
|
|
|
+ {
|
|
|
+ return Response::json('Todo not found', 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ Session::flash('message', 'Successfully deleted the nerd!');
|
|
|
+ return Redirect::to('athome');
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+希望你能读懂,没有的话,继续。
|
|
|
|
|
|
下面这部分来自于之前的博客,这里就不多加论述了。
|
|
|
这个也就是我们要的模板,
|
|
|
|
|
|
###修改Create()
|
|
|
|
|
|
-<pre><code class="php">
|
|
|
- public function create()
|
|
|
- {
|
|
|
- $maxid=Athomes::max('id');
|
|
|
- return View::make('athome.create')->with('maxid',$maxid);
|
|
|
- }
|
|
|
-</code></pre>
|
|
|
+```php
|
|
|
+public function create()
|
|
|
+{
|
|
|
+ $maxid=Athomes::max('id');
|
|
|
+ return View::make('athome.create')->with('maxid',$maxid);
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
|
|
|
这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为
|
|
|
|
|
|
-<pre><code class="php">
|
|
|
- public function create()
|
|
|
- {
|
|
|
- return View::make('athome.create');
|
|
|
- }
|
|
|
-</code></pre>
|
|
|
+```php
|
|
|
+public function create()
|
|
|
+{
|
|
|
+ return View::make('athome.create');
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
这里只是对其中代码的进行一下说明。
|
|
|
|
|
@@ -377,53 +411,58 @@ app/model 加个 class
|
|
|
|
|
|
由于使用到了bootstrap以及bootstrap-select,记得添加css。
|
|
|
|
|
|
- <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
|
|
|
- <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />
|
|
|
+```html
|
|
|
+<link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
|
|
|
+<link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />
|
|
|
+```
|
|
|
|
|
|
以及javascript
|
|
|
|
|
|
- <script type="text/javascript" src="<?= url('js/jquery.min.js')?>"></script>
|
|
|
- <script type="text/javascript" src="<?= url('js/bootstrap.min.js') ?>"></script>
|
|
|
- <script type="text/javascript" src="<?= url('js/bootstrap-select.min.js') ?>"></script>
|
|
|
- <script>
|
|
|
- $('.selectpicker').selectpicker();
|
|
|
- </script>
|
|
|
-
|
|
|
+```html
|
|
|
+<script type="text/javascript" src="<?= url('js/jquery.min.js')?>"></script>
|
|
|
+<script type="text/javascript" src="<?= url('js/bootstrap.min.js') ?>"></script>
|
|
|
+<script type="text/javascript" src="<?= url('js/bootstrap-select.min.js') ?>"></script>
|
|
|
+<script>
|
|
|
+$('.selectpicker').selectpicker();
|
|
|
+</script>
|
|
|
+```
|
|
|
|
|
|
####创建表单
|
|
|
|
|
|
这里用到的是之前提到的那个作者写下的,稍微修改了一下。
|
|
|
|
|
|
- <div class="row-fluid">
|
|
|
- {{ HTML::ul($errors->all()) }}
|
|
|
- {{ Form::open(array('url' => 'athome')) }}
|
|
|
+```html
|
|
|
+<div class="row-fluid">
|
|
|
+ {{ HTML::ul($errors->all()) }}
|
|
|
+ {{ Form::open(array('url' => 'athome')) }}
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('led1', '开关1') }}
|
|
|
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('led1', '开关1') }}
|
|
|
+ {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
|
|
|
|
|
|
- </div>
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('sensors1', 'sensors1') }}
|
|
|
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('sensors1', 'sensors1') }}
|
|
|
+ {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('sensors2', 'sensors2') }}
|
|
|
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('sensors2', 'sensors2') }}
|
|
|
+ {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('temperature', 'temperature') }}
|
|
|
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('temperature', 'temperature') }}
|
|
|
+ {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
- {{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
|
|
|
+ {{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
|
|
|
|
|
|
- {{ Form::close() }}
|
|
|
+ {{ Form::close() }}
|
|
|
|
|
|
- </div>
|
|
|
+ </div>
|
|
|
+```
|
|
|
|
|
|
开关一开始打算用 checkbox,加上 bootstrap-switch 实现
|
|
|
ON OFF
|
|
@@ -431,116 +470,121 @@ app/model 加个 class
|
|
|
|
|
|
还需要修改一下之前的 create(),添加一行
|
|
|
|
|
|
- return Redirect::to('athome');
|
|
|
-
|
|
|
- 也就是添加完后,重定向到首页查看,最后例子给出的 create 如下
|
|
|
-
|
|
|
- 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();
|
|
|
-
|
|
|
- Session::flash('message', 'Successfully created athome!');
|
|
|
- return Redirect::to('athome');
|
|
|
- }
|
|
|
+```php
|
|
|
+return Redirect::to('athome');
|
|
|
+```
|
|
|
+
|
|
|
+也就是添加完后,重定向到首页查看,最后例子给出的 create 如下
|
|
|
+
|
|
|
+```php
|
|
|
+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();
|
|
|
+
|
|
|
+ Session::flash('message', 'Successfully created athome!');
|
|
|
+ return Redirect::to('athome');
|
|
|
}
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
###编辑模板
|
|
|
|
|
|
完整的 blade 模板文件
|
|
|
|
|
|
- <!DOCTYPE html lang="zh-cn">
|
|
|
- <html>
|
|
|
- <head>
|
|
|
- <meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
|
- <meta name="keywords" content="">
|
|
|
- <meta name="viewport" content="width=device-width">
|
|
|
- <meta name="description" content="">
|
|
|
- <title>@yield('title')</title>
|
|
|
- <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
|
|
|
- <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />
|
|
|
- <link rel="stylesheet" href="<?= url('css/justified-nav.css') ?>" type="text/css" media="screen" />
|
|
|
- </head>
|
|
|
- <body>
|
|
|
+```html
|
|
|
+<!DOCTYPE html lang="zh-cn">
|
|
|
+<html>
|
|
|
+ <head>
|
|
|
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
|
+ <meta name="keywords" content="">
|
|
|
+ <meta name="viewport" content="width=device-width">
|
|
|
+ <meta name="description" content="">
|
|
|
+ <title>@yield('title')</title>
|
|
|
+ <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
|
|
|
+ <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />
|
|
|
+ <link rel="stylesheet" href="<?= url('css/justified-nav.css') ?>" type="text/css" media="screen" />
|
|
|
+ </head>
|
|
|
+<body>
|
|
|
|
|
|
|
|
|
- <div class="container">
|
|
|
+<div class="container">
|
|
|
|
|
|
- <div class="container">
|
|
|
- <div class="row-fluid">
|
|
|
+<div class="container">
|
|
|
+ <div class="row-fluid">
|
|
|
|
|
|
- <h1>Edit {{ $athome->id }}</h1>
|
|
|
+<h1>Edit {{ $athome->id }}</h1>
|
|
|
|
|
|
- <!-- if there are creation errors, they will show here -->
|
|
|
- {{ HTML::ul($errors->all()) }}
|
|
|
+<!-- if there are creation errors, they will show here -->
|
|
|
+{{ HTML::ul($errors->all()) }}
|
|
|
|
|
|
- {{ Form::model($athome, array('route' => array('athome.update', $athome->id), 'method' => 'PUT')) }}
|
|
|
+{{ Form::model($athome, array('route' => array('athome.update', $athome->id), 'method' => 'PUT')) }}
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('led1', '开关1') }}
|
|
|
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('led1', '开关1') }}
|
|
|
+ {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
|
|
|
|
|
|
- </div>
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('sensors1', '传感器1') }}
|
|
|
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('sensors1', '传感器1') }}
|
|
|
+ {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('sensors2', '传感器2') }}
|
|
|
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('sensors2', '传感器2') }}
|
|
|
+ {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
- <div class="form-group">
|
|
|
- {{ Form::label('temperature', '温度传感器') }}
|
|
|
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
|
|
|
- </div>
|
|
|
+ <div class="form-group">
|
|
|
+ {{ Form::label('temperature', '温度传感器') }}
|
|
|
+ {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
|
|
|
+ </div>
|
|
|
|
|
|
|
|
|
- {{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }}
|
|
|
+ {{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }}
|
|
|
|
|
|
- {{ Form::close() }}
|
|
|
+{{ Form::close() }}
|
|
|
|
|
|
- </div>
|
|
|
</div>
|
|
|
+</div>
|
|
|
|
|
|
|
|
|
- <div class="footer">
|
|
|
- <p>© Company 2013</p>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
-
|
|
|
- </div>
|
|
|
- <script type="text/javascript" src="<?= url('js/jquery.min.js')?>"></script>
|
|
|
- <script type="text/javascript" src="<?= url('js/bootstrap.min.js') ?>"></script>
|
|
|
- <script type="text/javascript" src="<?= url('js/bootstrap-select.min.js') ?>"></script>
|
|
|
- <script>
|
|
|
- $('.selectpicker').selectpicker();
|
|
|
- </script>
|
|
|
- <script type="text/javascript" src="<?= url('js/log.js') ?>"></script>
|
|
|
+<div class="footer">
|
|
|
+ <p>© Company 2013</p>
|
|
|
+ </div>
|
|
|
+</div>
|
|
|
|
|
|
+</div>
|
|
|
+<script type="text/javascript" src="<?= url('js/jquery.min.js')?>"></script>
|
|
|
+<script type="text/javascript" src="<?= url('js/bootstrap.min.js') ?>"></script>
|
|
|
+<script type="text/javascript" src="<?= url('js/bootstrap-select.min.js') ?>"></script>
|
|
|
+<script>
|
|
|
+ $('.selectpicker').selectpicker();
|
|
|
+ </script>
|
|
|
+<script type="text/javascript" src="<?= url('js/log.js') ?>"></script>
|
|
|
|
|
|
- </body>
|
|
|
- </html>
|
|
|
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+```
|
|
|
|
|
|
最后效果见:[http://b.phodal.com/][bphodal]
|
|
|
|