所以我想检查数据库是否在 laravel 5 中连接。我为此使用了中间件。
中间件代码
中间件.php
命名空间应用\Http\中间件;
use Closure;
class MyMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(DB::connection()->getDatabaseName())
{
echo "conncted sucessfully to database ".DB::connection()->getDatabaseName();
}else{
die("Couldn't connect");
}
return $next($request);
}
}
将其添加到 kernel.php
protected $routeMiddleware = [
'error' => 'App\Http\Middleware\MyMiddleware',
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
我想检查特定路线是否有错误
Route::get('/wow', ['middleware' => 'error', function () {
//
}]);
中间件一切正常,但没有类或 laravel 核心功能,如 check db 正在工作。我该如何解决?
这是 laravel 显示的错误
FatalErrorException in MyMiddleware.php line 18:
Class 'App\Http\Middleware\DB' not found