Blogger Information
Blog 64
fans 6
comment 2
visits 83032
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel框架的基本用法,新建控制器,新建路由,渲染视图,数据库的增删改查
王娇
Original
896 people have browsed it

学习总结

1.在当前项目下使用artisan命令创建控制器,快速高效不容易出错

2.在控制器中向页面传输数据时,$result['res']=$data; , return view('home/goods',$result); ,传输到goods.blade.php页面中的数据是echo $res

3.laravel框架中每次访问页面必须经过路由

1.新建goodCon和indexCon控制器,在home文件夹下

  • 在当前文件夹下打开终端,输入以下命令

    • 新建indexCon控制器

      • php artisan make:controller home/goodsCon
    • 新建goodsCon控制器

      • php artisan make:controller home/indexCon
  • 新建控制器后,目录结构如下

2.新建路由 routes->web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Web Routes
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here is where you can register web routes for your application. These
  9. | routes are loaded by the RouteServiceProvider within a group which
  10. | contains the "web" middleware group. Now create something great!
  11. |
  12. */
  13. //get方法的第一个参数'/'代表网站的入口地址,第二个参数访问的是app/http/controllers/home下的indexCon控制器的index方法
  14. Route::get('/', 'home\indexCon@index');
  15. //get方法的第一个参数'/home/article'访问地址为backstage.com/goods,第二个参数访问的是app/http/controllers/home下的goodsCon控制器的index方法
  16. Route::get('/goods','home\goodsCon@index');
  17. //添加商品
  18. Route::get('/goods/add','home\goodsCon@addGoods');
  19. //删除商品
  20. Route::get('/goods/del','home\goodsCon@delGoods');
  21. //更新商品
  22. Route::get('/goods/update','home\goodsCon@updateGoods');

3.添加控制器方法 app->Http->controllers->home->goodsCon.php

  1. <?php
  2. namespace App\Http\Controllers\home;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use DB;
  6. class goodsCon extends Controller
  7. {
  8. //
  9. public function index()
  10. {
  11. return $this->showGoods();
  12. }
  13. //显示商品
  14. public function showGoods()
  15. {
  16. $res = DB::select('select * from tb_goods');
  17. $data = [];
  18. foreach($res as $key=>$val):
  19. $data[] = (array)$val;
  20. endforeach;
  21. //注意goods.blade.php文件接收的是$result数组中的键名为res的这个变量的值
  22. $result['res']=$data;
  23. return view('home/goods',$result);
  24. }
  25. //添加商品
  26. public function addGoods()
  27. {
  28. $date = date('Y-m-d');
  29. $res = DB::insert("insert into `tb_goods` (`name`,`price`,`unit`,`sdate`) values ('黄桃','10','斤','$date') ");
  30. echo $res;
  31. }
  32. //删除商品
  33. public function delGoods()
  34. {
  35. $res = DB::delete('delete from tb_goods where id>22');
  36. echo $res;
  37. }
  38. //更新商品
  39. public function updateGoods()
  40. {
  41. $res = DB::update('update tb_goods set name="西瓜" where id="22"');
  42. echo $res;
  43. }
  44. }

4.添加视图文件,渲染数据resources->vies->home->goods.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>前台商品展示页</title>
  7. <link rel="stylesheet" href="layui/css/layui.css">
  8. <script src="layui/layui.js"></script>
  9. </head>
  10. <body>
  11. <h2>商品展示页</h2>
  12. <table class="layui-table">
  13. <thead>
  14. <tr>
  15. <td>商品编号</td>
  16. <td>商品名称</td>
  17. <td>商品单价</td>
  18. <td>商品单位</td>
  19. <td>发布日期</td>
  20. <td>操作</td>
  21. </tr>
  22. </thead>
  23. <tbody>
  24. <?php
  25. /* $res 这个变量的值是来自showCon控制器中 */
  26. foreach($res as $goods):
  27. ?>
  28. <tr>
  29. <td><?php echo $goods['id'];?></td>
  30. <td><?php echo $goods['name'];?></td>
  31. <td><?php echo $goods['price'];?></td>
  32. <td><?php echo $goods['unit'];?></td>
  33. <td><?php echo $goods['sdate'];?></td>
  34. <td><button class="layui-btn">修改</button><button class="layui-btn">删除</button></td>
  35. </tr>
  36. <?php
  37. endforeach;
  38. ?>
  39. </tbody>
  40. </table>
  41. </body>
  42. </html>

5.代码显示如果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:如果前面掌握了, 这里应该是水到渠成的事
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post