Created
February 6, 2020 19:47
-
-
Save elminson/c8198fa08e06e65f5428f124444f509c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allow pretty print with the query parameter ?pretty=true | |
*/ | |
declare(strict_types=1); | |
namespace App\Http\Middleware; | |
use Illuminate\Http\JsonResponse; | |
/** | |
* Class PrettyPrintMiddleware | |
* @package App\Http\Middleware | |
*/ | |
class PrettyPrintMiddleware | |
{ | |
/** | |
* @var string the query parameter | |
*/ | |
const QUERY_PARAMETER = 'pretty'; | |
/** | |
* Apply pretty print if designated | |
* | |
* @param $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, \Closure $next) | |
{ | |
$response = $next($request); | |
if ($response instanceof JsonResponse) { | |
if ($request->query(self::QUERY_PARAMETER) == 'true') { | |
$response->setEncodingOptions(JSON_PRETTY_PRINT); | |
} | |
} | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment