使用after middleware转换数据时:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class JsonMiddleware
{
/**
* Handle an incoming request.
* @param Request $request
* @param Closure|callable $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
dd($response);
return $response;
}
}
Kernel.php
<?php
namespace App\Http;
use App\Http\Middleware\JsonMiddleware;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
JsonMiddleware::class,
];
protected $routeMiddleware = [
];
}
异常\处理程序
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
];
public function report(Exception $e)
{
return parent::report($e);
}
public function render($request, Exception $e)
{
return response($e->getMessage());
}
}
routes.php文件
<?php
use App\Exceptions\FunException;
Route::get('/', function () {
return response('This should be displayed as JSON payload - code 200');
});
Route::get('/fun', function () {
throw new FunException('This is a fun exception, code 433');
});
引发异常时dd($response)
从未运行过,我可以更改吗?