Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:中间件之前要前置方法或请求拦截器, 不是新东西
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
//重新定向数据库表文件
protected $table='article';
}
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Article;
class Home extends Controller
{
//model 模型
public function mymodels(Article $article){
echo '<br> mymodel.blade执行了...';
//将返回的模型类转为数组
$res=$article->get()->toArray();
// echo '<pre>';
// print_r($res);
return view ('mymodel',['items'=>$res]);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/layui/css/layui.css" />
<title>模型、中间件示例</title>
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<td>ID</td>
<td>CATE_ID</td>
<td>标题</td>
</tr>
</thead>
<tbody>
@foreach ($items as $val)
<tr>
<td>{{$val['id']}}</td>
<td>{{$val['cate_id']}}</td>
<td>{{$val['title']}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
命令行 php artisan make:middleware Mymiddle
<?php
namespace App\Http\Middleware;
use Closure;
class Mymiddle
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
echo "这是一个中间件...";
return $next($request);
}
}
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'mymiddle' => \App\Http\Middleware\Mymiddle::class,
];
Route::get('/dbmodels','home@mymodels')->middleware('mymiddle');
效果图
总结:
1、Laravel 模型中需重映射数据库表文件。
2、中间件需 创建-注册-触发。
3、命令行 创建模型、中间件等,需要把当前目录定为artisan所在目录。