1
0

2.3.create_laravel.md.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Style-Type" content="text/css" />
  6. <meta name="generator" content="pandoc" />
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <style type="text/css">
  10. div.sourceCode { overflow-x: auto; }
  11. table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  12. margin: 0; padding: 0; vertical-align: baseline; border: none; }
  13. table.sourceCode { width: 100%; line-height: 100%; }
  14. td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
  15. td.sourceCode { padding-left: 5px; }
  16. code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
  17. code > span.dt { color: #902000; } /* DataType */
  18. code > span.dv { color: #40a070; } /* DecVal */
  19. code > span.bn { color: #40a070; } /* BaseN */
  20. code > span.fl { color: #40a070; } /* Float */
  21. code > span.ch { color: #4070a0; } /* Char */
  22. code > span.st { color: #4070a0; } /* String */
  23. code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
  24. code > span.ot { color: #007020; } /* Other */
  25. code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
  26. code > span.fu { color: #06287e; } /* Function */
  27. code > span.er { color: #ff0000; font-weight: bold; } /* Error */
  28. code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  29. code > span.cn { color: #880000; } /* Constant */
  30. code > span.sc { color: #4070a0; } /* SpecialChar */
  31. code > span.vs { color: #4070a0; } /* VerbatimString */
  32. code > span.ss { color: #bb6688; } /* SpecialString */
  33. code > span.im { } /* Import */
  34. code > span.va { color: #19177c; } /* Variable */
  35. code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  36. code > span.op { color: #666666; } /* Operator */
  37. code > span.bu { } /* BuiltIn */
  38. code > span.ex { } /* Extension */
  39. code > span.pp { color: #bc7a00; } /* Preprocessor */
  40. code > span.at { color: #7d9029; } /* Attribute */
  41. code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
  42. code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  43. code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  44. code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  45. </style>
  46. <link rel="stylesheet" href="css/vendor.css" type="text/css" />
  47. </head>
  48. <body>
  49. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
  50. <head>
  51. <title>一步步搭建物联网系统(教你设计物联网系统)</title>
  52. <meta name="keywords" content="设计物联网系统,物联网系统设计">
  53. <meta name="description" content="一份关于如何设计物联网系统的文档">
  54. <link rel="stylesheet" href="../css/vendor.css" />
  55. </head>
  56. <body class="container inner wrap">
  57. <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
  58. </body>
  59. </html>
  60. <div id="TOC">
  61. <ul>
  62. <li><a href="#创建rest服务"><span class="toc-section-number">1</span> 创建REST服务</a><ul>
  63. <li><a href="#数据库迁移"><span class="toc-section-number">1.1</span> 数据库迁移</a><ul>
  64. <li><a href="#创建表"><span class="toc-section-number">1.1.1</span> 创建表</a></li>
  65. <li><a href="#数据库迁移-1"><span class="toc-section-number">1.1.2</span> 数据库迁移</a></li>
  66. </ul></li>
  67. <li><a href="#创建restful"><span class="toc-section-number">1.2</span> 创建RESTful</a></li>
  68. <li><a href="#laravel-resources"><span class="toc-section-number">1.3</span> Laravel Resources</a><ul>
  69. <li><a href="#修改create"><span class="toc-section-number">1.3.1</span> 修改Create()</a></li>
  70. <li><a href="#创建表单"><span class="toc-section-number">1.3.2</span> 创建表单</a></li>
  71. <li><a href="#编辑模板"><span class="toc-section-number">1.3.3</span> 编辑模板</a></li>
  72. </ul></li>
  73. </ul></li>
  74. </ul>
  75. </div>
  76. <h1 id="创建rest服务"><span class="header-section-number">1</span> 创建REST服务</h1>
  77. <h2 id="数据库迁移"><span class="header-section-number">1.1</span> 数据库迁移</h2>
  78. <p>这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。</p>
  79. <h3 id="创建表"><span class="header-section-number">1.1.1</span> 创建表</h3>
  80. <p>表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。</p>
  81. <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>
  82. <p>打开 app/database/migrations/<strong><em>create_athomes_table.php这里的</em></strong>是由日期和某些东西组成的,修改生成的代码为下面。</p>
  83. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">use</span> Illuminate\Database\Schema\Blueprint<span class="ot">;</span>
  84. <span class="kw">use</span> Illuminate\Database\Migrations\Migration<span class="ot">;</span>
  85. <span class="kw">class</span> CreateAthomesTable <span class="kw">extends</span> Migration {
  86. <span class="kw">public</span> <span class="kw">function</span> up<span class="ot">()</span>
  87. {
  88. Schema::create<span class="ot">(</span><span class="st">&#39;athomes&#39;</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>Blueprint <span class="kw">$table</span><span class="ot">)</span>
  89. {
  90. <span class="kw">$table</span>---&gt;increments<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  91. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  92. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  93. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  94. <span class="kw">$table</span>-&gt;boolean<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  95. <span class="kw">$table</span>-&gt;timestamps<span class="ot">();</span>
  96. }<span class="ot">);</span>
  97. }
  98. <span class="kw">public</span> <span class="kw">function</span> down<span class="ot">()</span>
  99. {
  100. Schema::drop<span class="ot">(</span><span class="st">&#39;athomes&#39;</span><span class="ot">);</span>
  101. }
  102. }</code></pre></div>
  103. <p>意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。</p>
  104. <h3 id="数据库迁移-1"><span class="header-section-number">1.1.2</span> 数据库迁移</h3>
  105. <p>我们只是写了我们需要的数据的格式而并没有丢到数据库里,</p>
  106. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate</code></pre></div>
  107. <p>这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。</p>
  108. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre></div>
  109. <div class="sourceCode"><pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">use</span> iot;
  110. <span class="kw">select</span> * <span class="kw">from</span> athomes;</code></pre></div>
  111. <p>就可以看到我们写的东西,那么接下来就是创建RESTful服务了</p>
  112. <h2 id="创建restful"><span class="header-section-number">1.2</span> 创建RESTful</h2>
  113. <p>用下面的代码实现我们称之为Athomes控制器的创建</p>
  114. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan controller:make AthomesController </code></pre></div>
  115. <p>就会在app/controllers下面生成下面的代码</p>
  116. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  117. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span> {}
  118. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span> {}
  119. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span> {}
  120. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  121. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  122. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  123. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  124. }</code></pre></div>
  125. <h2 id="laravel-resources"><span class="header-section-number">1.3</span> Laravel Resources</h2>
  126. <p>上面的代码过于沉重,请让我用 Ctrl+C 来带来点知识吧。</p>
  127. <table>
  128. <thead>
  129. <tr class="header">
  130. <th align="left">Verb</th>
  131. <th align="left">Path</th>
  132. <th align="left">Action</th>
  133. <th align="left">Route Name</th>
  134. </tr>
  135. </thead>
  136. <tbody>
  137. <tr class="odd">
  138. <td align="left">GET</td>
  139. <td align="left">/resource</td>
  140. <td align="left">index</td>
  141. <td align="left">resource.index</td>
  142. </tr>
  143. <tr class="even">
  144. <td align="left">GET</td>
  145. <td align="left">/resource/create</td>
  146. <td align="left">create</td>
  147. <td align="left">resource.create</td>
  148. </tr>
  149. <tr class="odd">
  150. <td align="left">POST</td>
  151. <td align="left">/resource</td>
  152. <td align="left">store</td>
  153. <td align="left">resource.store</td>
  154. </tr>
  155. <tr class="even">
  156. <td align="left">GET</td>
  157. <td align="left">/resource/{resource}</td>
  158. <td align="left">show</td>
  159. <td align="left">resource.show</td>
  160. </tr>
  161. <tr class="odd">
  162. <td align="left">GET</td>
  163. <td align="left">/resource/{resource}/edit</td>
  164. <td align="left">edit</td>
  165. <td align="left">resource.edit</td>
  166. </tr>
  167. <tr class="even">
  168. <td align="left">PUT/PATCH</td>
  169. <td align="left">/resource/{resource}</td>
  170. <td align="left">update</td>
  171. <td align="left">resource.update</td>
  172. </tr>
  173. <tr class="odd">
  174. <td align="left">DELETE</td>
  175. <td align="left">/resource/{resource}</td>
  176. <td align="left">destroy</td>
  177. <td align="left">resource.destroy</td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. <p>所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model 加个 class</p>
  182. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> Athomes <span class="kw">extends</span> Eloquent {
  183. <span class="kw">protected</span> <span class="kw">$table</span> = <span class="st">&#39;athomes&#39;</span><span class="ot">;</span>
  184. }</code></pre></div>
  185. <p>如果你想要的只是控制器Athomes的代码的话。。</p>
  186. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  187. <span class="kw">public</span> <span class="kw">$restful</span>=<span class="kw">true</span><span class="ot">;</span>
  188. <span class="kw">protected</span> <span class="kw">$athome</span><span class="ot">;</span>
  189. <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>
  190. {
  191. <span class="kw">$this</span>---&gt;athome = <span class="kw">$athome</span> <span class="ot">;</span>
  192. }
  193. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span>
  194. {
  195. <span class="kw">$maxid</span>=Athomes::all<span class="ot">();</span>
  196. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  197. }
  198. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  199. {
  200. <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  201. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">)</span>-&gt;with<span class="ot">(</span><span class="st">&#39;maxid&#39;</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
  202. }
  203. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
  204. {
  205. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  206. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  207. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  208. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  209. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  210. <span class="ot">);</span>
  211. <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>
  212. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  213. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  214. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">)</span>
  215. -&gt;withInput<span class="ot">(</span>Input::except<span class="ot">(</span><span class="st">&#39;password&#39;</span><span class="ot">));</span>
  216. } <span class="kw">else</span> {
  217. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  218. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  219. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  220. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  221. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  222. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  223. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  224. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  225. }
  226. }
  227. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  228. {
  229. <span class="kw">$myid</span>=Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  230. <span class="kw">$maxid</span>=Athomes::where<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">,</span><span class="st">&#39;=&#39;</span><span class="ot">,</span><span class="kw">$id</span><span class="ot">)</span>
  231. -&gt;select<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">,</span><span class="st">&#39;temperature&#39;</span><span class="ot">,</span><span class="st">&#39;sensors1&#39;</span><span class="ot">,</span><span class="st">&#39;sensors2&#39;</span><span class="ot">,</span><span class="st">&#39;led1&#39;</span><span class="ot">)</span>
  232. -&gt;get<span class="ot">();</span>
  233. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  234. }
  235. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  236. {
  237. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  238. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.edit&#39;</span><span class="ot">)</span>
  239. -&gt;with<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">,</span> <span class="kw">$athome</span><span class="ot">);</span>
  240. }
  241. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  242. {
  243. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  244. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required|&#39;</span><span class="ot">,</span>
  245. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  246. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  247. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  248. <span class="ot">);</span>
  249. <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>
  250. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  251. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/&#39;</span> . <span class="kw">$id</span> . <span class="st">&#39;/edit&#39;</span><span class="ot">)</span>
  252. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  253. } <span class="kw">else</span> {
  254. <span class="kw">$nerd</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  255. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  256. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  257. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  258. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  259. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  260. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  261. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  262. }
  263. }
  264. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  265. {
  266. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  267. <span class="kw">$athome</span>-&gt;delete<span class="ot">();</span>
  268. <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>
  269. {
  270. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="st">&#39;Todo not found&#39;</span><span class="ot">,</span> <span class="dv">404</span><span class="ot">);</span>
  271. }
  272. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully deleted the nerd!&#39;</span><span class="ot">);</span>
  273. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  274. }
  275. }</code></pre></div>
  276. <p>希望你能读懂,没有的话,继续。</p>
  277. <p>下面这部分来自于之前的博客,这里就不多加论述了。 这个也就是我们要的模板,</p>
  278. <h3 id="修改create"><span class="header-section-number">1.3.1</span> 修改Create()</h3>
  279. <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>
  280. {
  281. <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  282. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">)</span>-&gt;with<span class="ot">(</span><span class="st">&#39;maxid&#39;</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
  283. }</code></pre></div>
  284. <p>这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为</p>
  285. <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>
  286. {
  287. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">);</span>
  288. }</code></pre></div>
  289. <p>这里只是对其中代码的进行一下说明。</p>
  290. <h3 id="创建表单"><span class="header-section-number">1.3.2</span> 创建表单</h3>
  291. <h4 id="创建表单之前"><span class="header-section-number">1.3.2.1</span> 创建表单之前</h4>
  292. <p>由于使用到了bootstrap以及bootstrap-select,记得添加css。</p>
  293. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  294. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap-select.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span></code></pre></div>
  295. <p>以及javascript</p>
  296. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/jquery.min.js&#39;)?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  297. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  298. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap-select.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  299. <span class="kw">&lt;script&gt;</span>
  300. <span class="at">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
  301. <span class="op">&lt;</span><span class="ss">/script&gt;</span></code></pre></div>
  302. <h4 id="创建表单-1"><span class="header-section-number">1.3.2.2</span> 创建表单</h4>
  303. <p>这里用到的是之前提到的那个作者写下的,稍微修改了一下。</p>
  304. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;row-fluid&quot;</span><span class="kw">&gt;</span>
  305. {{ HTML::ul($errors-&gt;all()) }}
  306. {{ Form::open(array(&#39;url&#39; =&gt; &#39;athome&#39;)) }}
  307. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  308. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  309. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  310. <span class="kw">&lt;/div&gt;</span>
  311. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  312. {{ Form::label(&#39;sensors1&#39;, &#39;sensors1&#39;) }}
  313. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  314. <span class="kw">&lt;/div&gt;</span>
  315. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  316. {{ Form::label(&#39;sensors2&#39;, &#39;sensors2&#39;) }}
  317. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  318. <span class="kw">&lt;/div&gt;</span>
  319. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  320. {{ Form::label(&#39;temperature&#39;, &#39;temperature&#39;) }}
  321. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  322. <span class="kw">&lt;/div&gt;</span>
  323. {{ Form::submit(&#39;Create!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  324. {{ Form::close() }}
  325. <span class="kw">&lt;/div&gt;</span></code></pre></div>
  326. <p>开关一开始打算用 checkbox,加上 bootstrap-switch 实现 ON OFF 弱弱地觉得还是没掌握好的节奏,所以最后用 select 来实现。</p>
  327. <p>还需要修改一下之前的 create(),添加一行</p>
  328. <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">&#39;athome&#39;</span><span class="ot">);</span></code></pre></div>
  329. <p>也就是添加完后,重定向到首页查看,最后例子给出的 create 如下</p>
  330. <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>
  331. {
  332. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  333. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  334. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  335. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  336. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  337. <span class="ot">);</span>
  338. <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>
  339. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  340. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  341. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  342. } <span class="kw">else</span> {
  343. <span class="co">// store</span>
  344. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  345. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  346. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  347. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  348. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  349. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  350. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  351. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  352. }
  353. }</code></pre></div>
  354. <h3 id="编辑模板"><span class="header-section-number">1.3.3</span> 编辑模板</h3>
  355. <p>完整的 blade 模板文件</p>
  356. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html lang=&quot;zh-cn&quot;<span class="dt">&gt;</span>
  357. <span class="kw">&lt;html&gt;</span>
  358. <span class="kw">&lt;head&gt;</span>
  359. <span class="kw">&lt;meta</span><span class="ot"> http-equiv=</span><span class="st">&quot;Content-type&quot;</span><span class="ot"> content=</span><span class="st">&quot;text/html; charset=utf-8&quot;</span><span class="kw">&gt;</span>
  360. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;keywords&quot;</span><span class="ot"> content=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span>
  361. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;viewport&quot;</span><span class="ot"> content=</span><span class="st">&quot;width=device-width&quot;</span><span class="kw">&gt;</span>
  362. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;description&quot;</span><span class="ot"> content=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span>
  363. <span class="kw">&lt;title&gt;</span>@yield(&#39;title&#39;)<span class="kw">&lt;/title&gt;</span>
  364. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  365. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap-select.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  366. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/justified-nav.css&#39;) ?&gt;&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> media=</span><span class="st">&quot;screen&quot;</span> <span class="kw">/&gt;</span>
  367. <span class="kw">&lt;/head&gt;</span>
  368. <span class="kw">&lt;body&gt;</span>
  369. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  370. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  371. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;row-fluid&quot;</span><span class="kw">&gt;</span>
  372. <span class="kw">&lt;h1&gt;</span>Edit {{ $athome-&gt;id }}<span class="kw">&lt;/h1&gt;</span>
  373. <span class="co">&lt;!-- if there are creation errors, they will show here --&gt;</span>
  374. {{ HTML::ul($errors-&gt;all()) }}
  375. {{ Form::model($athome, array(&#39;route&#39; =&gt; array(&#39;athome.update&#39;, $athome-&gt;id), &#39;method&#39; =&gt; &#39;PUT&#39;)) }}
  376. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  377. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  378. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  379. <span class="kw">&lt;/div&gt;</span>
  380. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  381. {{ Form::label(&#39;sensors1&#39;, &#39;传感器1&#39;) }}
  382. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  383. <span class="kw">&lt;/div&gt;</span>
  384. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  385. {{ Form::label(&#39;sensors2&#39;, &#39;传感器2&#39;) }}
  386. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  387. <span class="kw">&lt;/div&gt;</span>
  388. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  389. {{ Form::label(&#39;temperature&#39;, &#39;温度传感器&#39;) }}
  390. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  391. <span class="kw">&lt;/div&gt;</span>
  392. {{ Form::submit(&#39;Edit the Nerd!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  393. {{ Form::close() }}
  394. <span class="kw">&lt;/div&gt;</span>
  395. <span class="kw">&lt;/div&gt;</span>
  396. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;footer&quot;</span><span class="kw">&gt;</span>
  397. <span class="kw">&lt;p&gt;</span>© Company 2013<span class="kw">&lt;/p&gt;</span>
  398. <span class="kw">&lt;/div&gt;</span>
  399. <span class="kw">&lt;/div&gt;</span>
  400. <span class="kw">&lt;/div&gt;</span>
  401. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/jquery.min.js&#39;)?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  402. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  403. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap-select.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  404. <span class="kw">&lt;script&gt;</span>
  405. <span class="at">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
  406. <span class="op">&lt;</span><span class="ss">/script&gt;</span>
  407. <span class="ss">&lt;script type=&quot;text/javascript</span><span class="st">&quot; src=&quot;</span><span class="op">&lt;?=</span> <span class="at">url</span>(<span class="st">&#39;js/log.js&#39;</span>) <span class="op">?&gt;</span><span class="st">&quot;&gt;&lt;/script&gt;</span>
  408. <span class="op">&lt;</span><span class="ss">/body&gt;</span>
  409. <span class="ss">&lt;/html</span><span class="op">&gt;</span></code></pre></div>
  410. <p>效果图:</p>
  411. <div class="figure">
  412. <img src="./images/edit.png" alt="Blade Edit" />
  413. <p class="caption">Blade Edit</p>
  414. </div>
  415. <p>最后效果见:<a href="http://b.phodal.com/" class="uri">http://b.phodal.com/</a></p>
  416. </body>
  417. </html>