Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active March 6, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niraj-shah/c2a7a678a6009741f139162032d9501a to your computer and use it in GitHub Desktop.
Save niraj-shah/c2a7a678a6009741f139162032d9501a to your computer and use it in GitHub Desktop.
Laravel 5.2 Middleware to trim all input
<?php
namespace App\Http\Middleware;
use Closure;
class InputTrim
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// credit to @t202wes - https://gist.github.com/drakakisgeo/3bba2a2600b4c554f836#gistcomment-1970006
$input = $request->all();
if ($input) {
array_walk_recursive($input, function (&$item) {
$item = trim($item);
$item = ($item == "") ? null : $item;
});
$request->merge($input);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment