Blogger Information
Blog 26
fans 2
comment 0
visits 24235
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel 控制器和视图模板创建
leverWang
Original
934 people have browsed it

1.手动创建控制器

  1. <?php
  2. namespace App\Http\Controllers;
  3. class UserController extends Controller
  4. {
  5. //
  6. }

2.在laravel根目录下创建控制台使用命令也可以创建控制器:
php artisan make:controller UserController

3.控制器往视图传值并输出:
定义路由: Route::get('demo/{id}', 'UserController@demo');:

  1. <?php
  2. namespace App\Http\Controllers;
  3. //引用request类
  4. use Illuminate\Http\Request;
  5. class userController extends Controller
  6. {
  7. // 直接输出参数
  8. public function demo(Request $request, $id)
  9. {
  10. $data[] = $id;
  11. $data[] = isset($request->name) ? $request->name : '';
  12. return $data;
  13. }
  14. }

测试:输入地址http://lv.php.cn/demo/3?name=jack,返回结果:{"id":"3","name":"jack"}

4.使用blade模板引擎输出
在resources\views目录下定义模板test.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>blade模板</title>
  7. <style>
  8. .show {
  9. font-size: 22px;
  10. color: #dc3958;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h3>直接绑定变量</h3>
  16. <P>ID : {{$id}}</P>
  17. <P>姓名 : {{$name}}</P>
  18. <P>年龄 : {{$age}}</P>
  19. <hr>
  20. <h3>使用if else条件判断 如果姓名是jack则把字体变色并放大</h3>
  21. @if($name=='jack')
  22. <P class="show">ID : {{$id}}</P>
  23. <P class="show">姓名 : {{$name}}</P>
  24. <P class="show">年龄 : {{$age}}</P>
  25. @else
  26. <P>ID : {{$id}}</P>
  27. <P>姓名 : {{$name}}</P>
  28. <P>年龄 : {{$age}}</P>
  29. @endif
  30. <hr>
  31. <h3>使用swich case 判断年龄并输出结果</h3>
  32. @switch (true)
  33. @case ($age>50)
  34. 老年
  35. @break
  36. @case ($age>30&&$age<=50)
  37. 壮年
  38. @break
  39. @case ($age>15&&$age<=30)
  40. 青年
  41. @break
  42. @case ($age>6&&$age<15)
  43. 少年
  44. @break
  45. @case ($age>0&&$age<=6)
  46. 幼年
  47. @break
  48. @default
  49. 未定义
  50. @endswitch
  51. </body>
  52. </html>

输出结果:

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