Blogger Information
Blog 36
fans 0
comment 0
visits 28444
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel控制器与路由的简单应用
phpcn_u202398
Original
876 people have browsed it

1、laravel控制器

代码示例——后端
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\controller;
  4. use Illuminate\Support\Facades\DB;
  5. class Index extends Controller{
  6. public function index(){
  7. return view("index");
  8. }
  9. //获取数据
  10. public function get(){
  11. $res = DB::select('select * from users ');
  12. $lists = [];
  13. foreach($res as $val){
  14. $lists[] = (array)$val;
  15. }
  16. $data['result'] = $lists;
  17. return view("index",$data);
  18. }
  19. //插入用户数据
  20. public function insert(){
  21. $data = time();
  22. $res = DB::insert('insert users(`name`,`pwd`,`email`,`add_time`)values("ccc","123456","asd@qq.com","$data")');
  23. print_r($res);
  24. }
  25. //更新数据
  26. public function update(){
  27. $data = time();
  28. $res = DB::update("update users set add_time = $data where id=5");
  29. var_dump($res);
  30. }
  31. //删除数据
  32. public function delete(){
  33. $res = DB::delete("delete from users where id=5");
  34. var_dump($res);
  35. }
  36. }
代码示例——前端
  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. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  7. <title>首页</title>
  8. </head>
  9. <body>
  10. <div style="margin:auto;width:500px">
  11. <table class="layui-table">
  12. <caption align="top">用户信息</caption>
  13. <thead>
  14. <tr>
  15. <th>ID</th>
  16. <th>用户名</th>
  17. <th>email</th>
  18. <th>添加时间</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <?php foreach($result as $val) { ?>
  23. <tr>
  24. <th><?php echo $val['id'] ?></th>
  25. <th><?php echo $val['name'] ?></th>
  26. <th><?php echo $val['email'] ?></th>
  27. <th><?php echo date("y-m-d H:i:s", $val['add_time']); ?></th>
  28. </tr>
  29. <?php } ?>
  30. </tbody>
  31. </table>
  32. </div>
  33. </body>
  34. </html>

2、路由

代码示例
  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. Route::get('/', function () {
  14. return view('welcome');
  15. });
  16. Route::get('/admin/index','admin\Index@index');
  17. Route::get('/admin/index/get','admin\Index@get');
  18. Route::get('/admin/index/insert','admin\Index@insert');
  19. Route::get('/admin/index/update','admin\Index@update');
  20. Route::get('/admin/index/delete','admin\Index@delete');

学习总结

本节课我们学习了laravel控制器与路由,通过本节课的学习知道了laravel控制器的使用以及学到了路由的使用,有助于后期的学习。

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