Blogger Information
Blog 119
fans 3
comment 1
visits 94673
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel MVC流程和数据库操作(原生)
赵大叔
Original
730 people have browsed it

laravel MVC流程

laravel框架任何请求都要经过路由。
public目录下有文件,其访问优先级高于路由。

视图文件加载语法: Route::get('/', function () {return view('welcome');});

  • welcome:视图文件名称。
  • view('welcome', 数组);: 数组参数,带一些数据到视图

视图文件加载演示代码:

  1. // test.php
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>加载视图文件测试</title>
  7. </head>
  8. <body>
  9. <div>
  10. 好好学习,天天向上!
  11. </div>
  12. </body>
  13. </html>
  14. // controller
  15. <?php
  16. namespace App\Http\Controllers;
  17. use Illuminate\Http\Request;
  18. class Home extends Controller
  19. {
  20. public function test(){
  21. return view('test');
  22. }
  23. }
  24. // 路由
  25. Route::get('/', function () {return view('test');});

演示效果图:
图1:

图2成功访问:

控制器映射到路由的语法: Route::get('路由地址', '控制器类@类方法名称');

  • 控制器类:默认是Controllers目录下,如果还有下级目录要带上。如Route::get('/admins/article/list', 'admins\Article@lists');

创建控制器:

  • 手动创建:新建文件( 带命名空间:命名空间名称和文件目录一致;控制器类名和文件名一致。)
  • artisian命令创建:进入artisian文件目录
    • php artisan make:controller 文件名:php artisan make:controller Home
    • 分目录创建:php artisan make:controller 目录名/文件名

使用命令创建成功图:

数据库操作(原生)

视图文件加载演示代码:

  1. // controlle
  2. <?php
  3. namespace App\Http\Controllers;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. // 控制器
  7. class Home extends Controller
  8. {
  9. // 加载test视图文件的方法
  10. public function index(){
  11. // echo '<pre>';
  12. $res = DB::select('SELECT * FROM `article`');
  13. // print_r($res);
  14. $lists = [];
  15. foreach ($res as $val){
  16. $lists[] = (array)$val;
  17. }
  18. $data['result'] = $lists;
  19. // print_r($data);
  20. return view('test', $data);
  21. }
  22. // 数据库查询(原生)
  23. public function select(){
  24. echo '<pre>';
  25. $res = DB::select('SELECT * FROM `article`');
  26. print_r($res);
  27. }
  28. // 数据库更新(原生)
  29. public function update(){
  30. echo '<pre>';
  31. $res = DB::update("UPDATE `article` SET `title` = '是的,我知道了' WHERE = `id` = 4 ");
  32. print_r($res);
  33. }
  34. // 数据库插入(原生)
  35. public function insert(){
  36. echo '<pre>';
  37. $res = DB::insert("INSERT `article` (`title`) VALUES ('php如何向文件中写入换行php如何向文件中写入换行'),
  38. ('PS精简版和完整版的区别PS精简版和完整版的区别')");
  39. print_r($res);
  40. }
  41. // 数据库删除(原生)
  42. public function delete(){
  43. echo '<pre>';
  44. $res = DB::delete("DELETE FROM `article` WHERE `id` = 4 ");
  45. print_r($res);
  46. }
  47. // 数据库删除(链式)
  48. public function finds(){
  49. echo '<pre>';
  50. $res = DB::table('article')->where('id', 2)->first();
  51. print_r($res);
  52. }
  53. }
  54. // 视图文件
  55. <!doctype html>
  56. <html lang="en">
  57. <head>
  58. <meta charset="UTF-8">
  59. <title>数据库操作</title>
  60. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  61. </head>
  62. <body>
  63. <table class="layui-table">
  64. <thead>
  65. <tr>
  66. <td>ID</td>
  67. <td>标题</td>
  68. </tr>
  69. </thead>
  70. <tbody>
  71. <?php foreach ($result as $val): ?>
  72. <tr>
  73. <td><?php echo $val['id']; ?></td>
  74. <td><?php echo $val['title']; ?></td>
  75. </tr>
  76. <?php endforeach; ?>
  77. </tbody>
  78. </table>
  79. </body>
  80. </html>
  81. // 路由
  82. <?php
  83. /*
  84. |--------------------------------------------------------------------------
  85. | Web Routes
  86. |--------------------------------------------------------------------------
  87. |
  88. | Here is where you can register web routes for your application. These
  89. | routes are loaded by the RouteServiceProvider within a group which
  90. | contains the "web" middleware group. Now create something great!
  91. |
  92. */
  93. // Route::get('/', function () {return view('welcome');});
  94. Route::get('/', function () {
  95. return view('test');
  96. });
  97. Route::get('/home/index', 'Home@index');
  98. // 数据库查询
  99. Route::get('/home/select', 'Home@select');
  100. // 数据库更新
  101. Route::get('/home/update', 'Home@update');
  102. // 数据库插入
  103. Route::get('/home/insert', 'Home@insert');
  104. // 数据库删除
  105. Route::get('/home/delete', 'Home@delete');
  106. // 数据库链式查询
  107. Route::get('/home/finds', 'Home@finds');

演示效果图:

总结

1、本节课学会了如何在MVC模型框架中把视图view展示出来。
2、了解如何把控制器映射到路由。
3、加强了一下原生的数据库增删改查操作
总之收获很多。

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