如何调用控制器中的客户错误,之后在模板中显示?
例如,我有以下条件:
if($id == 500){
// call error
}发布于 2017-03-05 23:11:50
如果您只想调用控制器中的错误并中止,您可以这样做
abort(500, 'Internal error');如果您想返回一个错误
return redirect()->back()->withErrors(['error' => 'was 500']);发布于 2017-03-05 23:15:02
您可以通过捕获处理程序 (app/Exceptions/Handler.php)中的错误来完成此操作,如下所示:
public function render($request, Exception $e){
// other errors here by your wish
// custom error message
if ($e instanceof \ErrorException) {
return response()->view('errors.500', [], 500);
}else{
return parent::render($request, $e);
}
return parent::render($request, $e);
}请注意,您需要在resources/views/error/500.blade.php中使用500个模板(如果不存在则创建,如果使用有关异常的数据或信息填充)
https://stackoverflow.com/questions/42609887
复制相似问题