Php 如何处理Laravel 5中的PDO异常
我已将Exceptions/Handler.php修改为:Php 如何处理Laravel 5中的PDO异常,php,laravel,pdo,eloquent,laravel-5.1,Php,Laravel,Pdo,Eloquent,Laravel 5.1,我已将Exceptions/Handler.php修改为: <?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Symfony\Comp
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler {
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException)
{
return response()->view('errors/404');
}
elseif ($e instanceof ErrorException) {
return response()->view('errors/404');
}
elseif ($e instanceof ModelNotFoundException) {
return response()->view('error_log(message)ors/404');
}
elseif($e instanceof PDOException)
{
return Redirect::to('install.php');
}
elseif($e instanceof QueryException)
{
return Redirect::to('install.php');
}
else return response()->view('errors/error');
}
我想我已经找到了解决方案:
放
use PDOException;
包括
使用异常
public function render($request, Exception $e)
{
if($e instanceof PDOException)
{
return response()->view('errors/404');
}
在异常/处理程序中
它对我有效这就是在Laravel 5.3中对我有效的内容(摘自/app/Exceptions/
中我的Handler.php
文件):
或者尝试{}catch(\pdotexception$pde){}
public function report(Exception $e)
{
// Both this, and below, are needed
if($e instanceof \PDOException) {
return response()->view('errors/404');
}
return parent::report($e);
}
public function render($request, Exception $e)
{
if($e instanceof \PDOException) {
// TODO send me an email!
Session::flash('messageclass', 'danger');
Session::flash('message', trans('general.pdoexception'));
return redirect()->back();
}
return parent::render($request, $e);
}