123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="Content-Style-Type" content="text/css" />
- <meta name="generator" content="pandoc" />
- <title></title>
- <style type="text/css">code{white-space: pre;}</style>
- <style type="text/css">
- div.sourceCode { overflow-x: auto; }
- table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
- margin: 0; padding: 0; vertical-align: baseline; border: none; }
- table.sourceCode { width: 100%; line-height: 100%; }
- td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
- td.sourceCode { padding-left: 5px; }
- code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
- code > span.dt { color: #902000; } /* DataType */
- code > span.dv { color: #40a070; } /* DecVal */
- code > span.bn { color: #40a070; } /* BaseN */
- code > span.fl { color: #40a070; } /* Float */
- code > span.ch { color: #4070a0; } /* Char */
- code > span.st { color: #4070a0; } /* String */
- code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
- code > span.ot { color: #007020; } /* Other */
- code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
- code > span.fu { color: #06287e; } /* Function */
- code > span.er { color: #ff0000; font-weight: bold; } /* Error */
- code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
- code > span.cn { color: #880000; } /* Constant */
- code > span.sc { color: #4070a0; } /* SpecialChar */
- code > span.vs { color: #4070a0; } /* VerbatimString */
- code > span.ss { color: #bb6688; } /* SpecialString */
- code > span.im { } /* Import */
- code > span.va { color: #19177c; } /* Variable */
- code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
- code > span.op { color: #666666; } /* Operator */
- code > span.bu { } /* BuiltIn */
- code > span.ex { } /* Extension */
- code > span.pp { color: #bc7a00; } /* Preprocessor */
- code > span.at { color: #7d9029; } /* Attribute */
- code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
- code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
- code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
- code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
- </style>
- <link rel="stylesheet" href="css/vendor.css" type="text/css" />
- </head>
- <body>
- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
- <head>
- <title>一步步搭建物联网系统(教你设计物联网系统)</title>
- <meta name="keywords" content="设计物联网系统,物联网系统设计">
- <meta name="description" content="一份关于如何设计物联网系统的文档">
- <link rel="stylesheet" href="../css/vendor.css" />
- </head>
- <body class="container inner wrap">
- <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
- </body>
- </html>
- <div id="TOC">
- <ul>
- <li><a href="#创建rest服务"><span class="toc-section-number">1</span> 创建REST服务</a><ul>
- <li><a href="#数据库迁移"><span class="toc-section-number">1.1</span> 数据库迁移</a><ul>
- <li><a href="#创建表"><span class="toc-section-number">1.1.1</span> 创建表</a></li>
- <li><a href="#数据库迁移-1"><span class="toc-section-number">1.1.2</span> 数据库迁移</a></li>
- </ul></li>
- <li><a href="#创建restful"><span class="toc-section-number">1.2</span> 创建RESTful</a></li>
- <li><a href="#laravel-resources"><span class="toc-section-number">1.3</span> Laravel Resources</a><ul>
- <li><a href="#修改create"><span class="toc-section-number">1.3.1</span> 修改Create()</a></li>
- <li><a href="#创建表单"><span class="toc-section-number">1.3.2</span> 创建表单</a></li>
- <li><a href="#编辑模板"><span class="toc-section-number">1.3.3</span> 编辑模板</a></li>
- </ul></li>
- </ul></li>
- </ul>
- </div>
- <h1 id="创建rest服务"><span class="header-section-number">1</span> 创建REST服务</h1>
- <h2 id="数据库迁移"><span class="header-section-number">1.1</span> 数据库迁移</h2>
- <p>这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。</p>
- <h3 id="创建表"><span class="header-section-number">1.1.1</span> 创建表</h3>
- <p>表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。</p>
- <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate:make create_athomes_table</code></pre></div>
- <p>打开 app/database/migrations/<strong><em>create_athomes_table.php这里的</em></strong>是由日期和某些东西组成的,修改生成的代码为下面。</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">use</span> Illuminate\Database\Schema\Blueprint<span class="ot">;</span>
- <span class="kw">use</span> Illuminate\Database\Migrations\Migration<span class="ot">;</span>
- <span class="kw">class</span> CreateAthomesTable <span class="kw">extends</span> Migration {
- <span class="kw">public</span> <span class="kw">function</span> up<span class="ot">()</span>
- {
- Schema::create<span class="ot">(</span><span class="st">'athomes'</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>Blueprint <span class="kw">$table</span><span class="ot">)</span>
- {
- <span class="kw">$table</span>--->increments<span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$table</span>->boolean<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$table</span>->timestamps<span class="ot">();</span>
- }<span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> down<span class="ot">()</span>
- {
- Schema::drop<span class="ot">(</span><span class="st">'athomes'</span><span class="ot">);</span>
- }
- }</code></pre></div>
- <p>意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。</p>
- <h3 id="数据库迁移-1"><span class="header-section-number">1.1.2</span> 数据库迁移</h3>
- <p>我们只是写了我们需要的数据的格式而并没有丢到数据库里,</p>
- <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate</code></pre></div>
- <p>这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。</p>
- <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre></div>
- <div class="sourceCode"><pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">use</span> iot;
- <span class="kw">select</span> * <span class="kw">from</span> athomes;</code></pre></div>
- <p>就可以看到我们写的东西,那么接下来就是创建RESTful服务了</p>
- <h2 id="创建restful"><span class="header-section-number">1.2</span> 创建RESTful</h2>
- <p>用下面的代码实现我们称之为Athomes控制器的创建</p>
- <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan controller:make AthomesController </code></pre></div>
- <p>就会在app/controllers下面生成下面的代码</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
- <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- }</code></pre></div>
- <h2 id="laravel-resources"><span class="header-section-number">1.3</span> Laravel Resources</h2>
- <p>上面的代码过于沉重,请让我用 Ctrl+C 来带来点知识吧。</p>
- <table>
- <thead>
- <tr class="header">
- <th align="left">Verb</th>
- <th align="left">Path</th>
- <th align="left">Action</th>
- <th align="left">Route Name</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">GET</td>
- <td align="left">/resource</td>
- <td align="left">index</td>
- <td align="left">resource.index</td>
- </tr>
- <tr class="even">
- <td align="left">GET</td>
- <td align="left">/resource/create</td>
- <td align="left">create</td>
- <td align="left">resource.create</td>
- </tr>
- <tr class="odd">
- <td align="left">POST</td>
- <td align="left">/resource</td>
- <td align="left">store</td>
- <td align="left">resource.store</td>
- </tr>
- <tr class="even">
- <td align="left">GET</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">show</td>
- <td align="left">resource.show</td>
- </tr>
- <tr class="odd">
- <td align="left">GET</td>
- <td align="left">/resource/{resource}/edit</td>
- <td align="left">edit</td>
- <td align="left">resource.edit</td>
- </tr>
- <tr class="even">
- <td align="left">PUT/PATCH</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">update</td>
- <td align="left">resource.update</td>
- </tr>
- <tr class="odd">
- <td align="left">DELETE</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">destroy</td>
- <td align="left">resource.destroy</td>
- </tr>
- </tbody>
- </table>
- <p>所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model 加个 class</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> Athomes <span class="kw">extends</span> Eloquent {
- <span class="kw">protected</span> <span class="kw">$table</span> = <span class="st">'athomes'</span><span class="ot">;</span>
- }</code></pre></div>
- <p>如果你想要的只是控制器Athomes的代码的话。。</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
- <span class="kw">public</span> <span class="kw">$restful</span>=<span class="kw">true</span><span class="ot">;</span>
- <span class="kw">protected</span> <span class="kw">$athome</span><span class="ot">;</span>
- <span class="kw">public</span> <span class="kw">function</span> <span class="fu">__construct</span><span class="ot">(</span>Athomes <span class="kw">$athome</span><span class="ot">)</span>
- {
- <span class="kw">$this</span>--->athome = <span class="kw">$athome</span> <span class="ot">;</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::all<span class="ot">();</span>
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">)</span>->with<span class="ot">(</span><span class="st">'maxid'</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/create'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">)</span>
- ->withInput<span class="ot">(</span>Input::except<span class="ot">(</span><span class="st">'password'</span><span class="ot">));</span>
- } <span class="kw">else</span> {
- <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }
- <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$myid</span>=Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$maxid</span>=Athomes::where<span class="ot">(</span><span class="st">'id'</span><span class="ot">,</span><span class="st">'='</span><span class="ot">,</span><span class="kw">$id</span><span class="ot">)</span>
- ->select<span class="ot">(</span><span class="st">'id'</span><span class="ot">,</span><span class="st">'temperature'</span><span class="ot">,</span><span class="st">'sensors1'</span><span class="ot">,</span><span class="st">'sensors2'</span><span class="ot">,</span><span class="st">'led1'</span><span class="ot">)</span>
- ->get<span class="ot">();</span>
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.edit'</span><span class="ot">)</span>
- ->with<span class="ot">(</span><span class="st">'athome'</span><span class="ot">,</span> <span class="kw">$athome</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required|'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/'</span> . <span class="kw">$id</span> . <span class="st">'/edit'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
- } <span class="kw">else</span> {
- <span class="kw">$nerd</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }
- <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$athome</span>->delete<span class="ot">();</span>
- <span class="kw">if</span><span class="ot">(</span><span class="fu">is_null</span><span class="ot">(</span><span class="kw">$athome</span><span class="ot">))</span>
- {
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="st">'Todo not found'</span><span class="ot">,</span> <span class="dv">404</span><span class="ot">);</span>
- }
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully deleted the nerd!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }</code></pre></div>
- <p>希望你能读懂,没有的话,继续。</p>
- <p>下面这部分来自于之前的博客,这里就不多加论述了。 这个也就是我们要的模板,</p>
- <h3 id="修改create"><span class="header-section-number">1.3.1</span> 修改Create()</h3>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">)</span>->with<span class="ot">(</span><span class="st">'maxid'</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
- }</code></pre></div>
- <p>这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">);</span>
- }</code></pre></div>
- <p>这里只是对其中代码的进行一下说明。</p>
- <h3 id="创建表单"><span class="header-section-number">1.3.2</span> 创建表单</h3>
- <h4 id="创建表单之前"><span class="header-section-number">1.3.2.1</span> 创建表单之前</h4>
- <p>由于使用到了bootstrap以及bootstrap-select,记得添加css。</p>
- <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap-select.min.css') ?>"</span> <span class="kw">/></span></code></pre></div>
- <p>以及javascript</p>
- <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/jquery.min.js')?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap-select.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script></span>
- <span class="at">$</span>(<span class="st">'.selectpicker'</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
- <span class="op"><</span><span class="ss">/script></span></code></pre></div>
- <h4 id="创建表单-1"><span class="header-section-number">1.3.2.2</span> 创建表单</h4>
- <p>这里用到的是之前提到的那个作者写下的,稍微修改了一下。</p>
- <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><div</span><span class="ot"> class=</span><span class="st">"row-fluid"</span><span class="kw">></span>
- {{ HTML::ul($errors->all()) }}
- {{ Form::open(array('url' => 'athome')) }}
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('led1', '开关1') }}
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors1', 'sensors1') }}
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors2', 'sensors2') }}
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('temperature', 'temperature') }}
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- {{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
- {{ Form::close() }}
- <span class="kw"></div></span></code></pre></div>
- <p>开关一开始打算用 checkbox,加上 bootstrap-switch 实现 ON OFF 弱弱地觉得还是没掌握好的节奏,所以最后用 select 来实现。</p>
- <p>还需要修改一下之前的 create(),添加一行</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span></code></pre></div>
- <p>也就是添加完后,重定向到首页查看,最后例子给出的 create 如下</p>
- <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/create'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
- } <span class="kw">else</span> {
- <span class="co">// store</span>
- <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }</code></pre></div>
- <h3 id="编辑模板"><span class="header-section-number">1.3.3</span> 编辑模板</h3>
- <p>完整的 blade 模板文件</p>
- <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html lang="zh-cn"<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><meta</span><span class="ot"> http-equiv=</span><span class="st">"Content-type"</span><span class="ot"> content=</span><span class="st">"text/html; charset=utf-8"</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"keywords"</span><span class="ot"> content=</span><span class="st">""</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"viewport"</span><span class="ot"> content=</span><span class="st">"width=device-width"</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"description"</span><span class="ot"> content=</span><span class="st">""</span><span class="kw">></span>
- <span class="kw"><title></span>@yield('title')<span class="kw"></title></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap-select.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/justified-nav.css') ?>"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> media=</span><span class="st">"screen"</span> <span class="kw">/></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"container"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"container"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"row-fluid"</span><span class="kw">></span>
- <span class="kw"><h1></span>Edit {{ $athome->id }}<span class="kw"></h1></span>
- <span class="co"><!-- if there are creation errors, they will show here --></span>
- {{ HTML::ul($errors->all()) }}
- {{ Form::model($athome, array('route' => array('athome.update', $athome->id), 'method' => 'PUT')) }}
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('led1', '开关1') }}
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors1', '传感器1') }}
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors2', '传感器2') }}
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('temperature', '温度传感器') }}
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- {{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }}
- {{ Form::close() }}
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"footer"</span><span class="kw">></span>
- <span class="kw"><p></span>© Company 2013<span class="kw"></p></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/jquery.min.js')?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap-select.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script></span>
- <span class="at">$</span>(<span class="st">'.selectpicker'</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
- <span class="op"><</span><span class="ss">/script></span>
- <span class="ss"><script type="text/javascript</span><span class="st">" src="</span><span class="op"><?=</span> <span class="at">url</span>(<span class="st">'js/log.js'</span>) <span class="op">?></span><span class="st">"></script></span>
- <span class="op"><</span><span class="ss">/body></span>
- <span class="ss"></html</span><span class="op">></span></code></pre></div>
- <p>效果图:</p>
- <div class="figure">
- <img src="./images/edit.png" alt="Blade Edit" />
- <p class="caption">Blade Edit</p>
- </div>
- <p>最后效果见:<a href="http://b.phodal.com/" class="uri">http://b.phodal.com/</a></p>
- </body>
- </html>
|