laravel blade模板引擎和中间件
home.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Article;
// 后台主页
class Home extends Controller{
//模型
public function mymodels(Article $article){
$res = $article->get()->toArray();
echo '<pre>';
print_r($res);
}
// blade 模板引擎
public function myblade(Article $article){
$myage =18;
$data['myage'] = $myage;
$data['name'] = '<span style="font-size:20px;">小明</span>';
$res = $article->get()->toArray();
$data['res'] = $res;
return view('myblade',$data);
}
}
blade模板
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
</head>
<body>
<!---原生-->
<?php if ($myage>=18){?>
<div>已成年<?php echo $name ?></div>
<div>已成年<?php echo htmlentities($name) ?></div>
<?php }else{?>
<div>未成年</div>
<?php }?>
<!---blade模板-->
@if($myage>=18)
<div style="color:red">已成年{{$name}}</div>
<div style="color:red">已成年{!!$name!!}</div>
@else
<div style="color:red">未成年</div>
@endif
<table class="layui-table">
<thead>
<tr><th>id</th><th>标题</th></tr>
</thead>
@foreach($res as $val)
<tr>
<td>{{$val['id']}}</td>
<td>{{$val['title']}}</td>
</tr>
@endforeach
</table>
<table class="layui-table">
<thead>
<tr><th>id</th><th>标题</th></tr>
</thead>
@for($i=0;$i<count($res);$i++)
<tr>
<td>{{$res[$i]['id']}}</td>
<td>{{$res[$i]['title']}}</td>
</tr>
@endfor
</table>
<table class="layui-table">
<thead>
<tr><th>id</th><th>标题</th></tr>
</thead>
<!--- while($val = array_pop($res)) --->
@while($val = array_shift($res))
<tr>
<td>{{$val['id']}}</td>
<td>{{$val['title']}}</td>
</tr>
@endwhile
</table>
</body>
</html>
中间件
需要三步
第一步:创建文件
在powershell中执行
php artisan make:middleware Mymiddle
执行后在laravel7\app\Http\Middleware中生成了Mymiddle.php。
第二步:注册
在laravel7\app\Http中有一个Kernel.php,在protected $routeMiddleware中加一行
'mymiddle' => \App\Http\Middleware\Mymiddle::class,
第三步:触发
修改路由,增加->middleware(‘mymiddle’)
例如:
修改前
Route::get('/dbmyblade','Home@myblade');
修改后
Route::get('/dbmyblade','Home@myblade')->middleware('mymiddle');