Blogger Information
Blog 14
fans 0
comment 1
visits 12856
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel blade模板引擎和中间件
王珂
Original
864 people have browsed it

laravel blade模板引擎和中间件

home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Article;
  6. // 后台主页
  7. class Home extends Controller{
  8. //模型
  9. public function mymodels(Article $article){
  10. $res = $article->get()->toArray();
  11. echo '<pre>';
  12. print_r($res);
  13. }
  14. // blade 模板引擎
  15. public function myblade(Article $article){
  16. $myage =18;
  17. $data['myage'] = $myage;
  18. $data['name'] = '<span style="font-size:20px;">小明</span>';
  19. $res = $article->get()->toArray();
  20. $data['res'] = $res;
  21. return view('myblade',$data);
  22. }
  23. }

blade模板

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  6. </head>
  7. <body>
  8. <!---原生-->
  9. <?php if ($myage>=18){?>
  10. <div>已成年<?php echo $name ?></div>
  11. <div>已成年<?php echo htmlentities($name) ?></div>
  12. <?php }else{?>
  13. <div>未成年</div>
  14. <?php }?>
  15. <!---blade模板-->
  16. @if($myage>=18)
  17. <div style="color:red">已成年{{$name}}</div>
  18. <div style="color:red">已成年{!!$name!!}</div>
  19. @else
  20. <div style="color:red">未成年</div>
  21. @endif
  22. <table class="layui-table">
  23. <thead>
  24. <tr><th>id</th><th>标题</th></tr>
  25. </thead>
  26. @foreach($res as $val)
  27. <tr>
  28. <td>{{$val['id']}}</td>
  29. <td>{{$val['title']}}</td>
  30. </tr>
  31. @endforeach
  32. </table>
  33. <table class="layui-table">
  34. <thead>
  35. <tr><th>id</th><th>标题</th></tr>
  36. </thead>
  37. @for($i=0;$i<count($res);$i++)
  38. <tr>
  39. <td>{{$res[$i]['id']}}</td>
  40. <td>{{$res[$i]['title']}}</td>
  41. </tr>
  42. @endfor
  43. </table>
  44. <table class="layui-table">
  45. <thead>
  46. <tr><th>id</th><th>标题</th></tr>
  47. </thead>
  48. <!--- while($val = array_pop($res)) --->
  49. @while($val = array_shift($res))
  50. <tr>
  51. <td>{{$val['id']}}</td>
  52. <td>{{$val['title']}}</td>
  53. </tr>
  54. @endwhile
  55. </table>
  56. </body>
  57. </html>

中间件

需要三步

第一步:创建文件

在powershell中执行

  1. php artisan make:middleware Mymiddle

执行后在laravel7\app\Http\Middleware中生成了Mymiddle.php。

第二步:注册

在laravel7\app\Http中有一个Kernel.php,在protected $routeMiddleware中加一行

  1. 'mymiddle' => \App\Http\Middleware\Mymiddle::class,

第三步:触发

修改路由,增加->middleware(‘mymiddle’)
例如:
修改前

  1. Route::get('/dbmyblade','Home@myblade');

修改后

  1. Route::get('/dbmyblade','Home@myblade')->middleware('mymiddle');
Correction status:Uncorrected

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!