Skip to content

Instantly share code, notes, and snippets.

@elminson
Created February 6, 2020 19:47
Show Gist options
  • Save elminson/c8198fa08e06e65f5428f124444f509c to your computer and use it in GitHub Desktop.
Save elminson/c8198fa08e06e65f5428f124444f509c to your computer and use it in GitHub Desktop.
<?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