Last active
March 6, 2017 12:04
-
-
Save niraj-shah/c2a7a678a6009741f139162032d9501a to your computer and use it in GitHub Desktop.
Laravel 5.2 Middleware to trim all input
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 | |
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