我正在升级另一个Laravel 4到5实例。现在,Exceptions中有一个文件“ExceptionServiceProvider”。使用以下代码:
<?
namespace KeepIt\Exceptions;
use Illuminate\Support\ServiceProvider;
class ExceptionServiceProvider extends ServiceProvider
{
public function register()
{
\App::error(function(AjaxException $exception)
{
$response = array(
'state' => false,
'message' => $exception->getErrorMessage(),
'errors' => $exception->getErrors(),
'type' => $exception->getType()
);
return \Response::json($response);
});
}
}
我知道App :: error在L5中不再有效,但是我没有让它重写这段代码。它不是404,它是500错误投掷。
答案 0 :(得分:1)
知道了...我只需将代码放到渲染功能所在的新Exceptions / Handler.php中。
/**
* 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 AjaxException){
$response = array(
'state' => false,
'message' => $e->getErrorMessage(),
'errors' => $e->getErrors(),
'type' => $e->getType()
);
return \Response::json($response);
}
return parent::render($request, $e);
}
答案 1 :(得分:0)
这个怎么样?
$status = 500 //Could be whatever you need
Response::json(['myMsg' => 'FooBar'], $status)
或
abort(404);