博主信息
博文 36
粉丝 0
评论 0
访问量 36601
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
laravel控制器与路由的简单应用
phpcn_u202398
原创
1087人浏览过

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控制器的使用以及学到了路由的使用,有助于后期的学习。

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:每一次访问必须通过路由, 包括首页
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学