Forked from drakakisgeo/gist:3bba2a2600b4c554f836
Last active
December 13, 2016 12:00
-
-
Save IlyaZha/a72ea944f1fb7daebbee9bbcefb97399 to your computer and use it in GitHub Desktop.
Auto trim all input [Laravel 5]
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
// Step 1. Create this class in the middleware folder (/app/Http/Middleware). | |
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
class BeforeAutoTrimmer { | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$inputs = $request->all(); | |
array_walk_recursive($inputs, function(&$input) { | |
$input = trim($input); | |
}); | |
$request->merge($inputs); | |
return $next($request); | |
} | |
} | |
// Step 2. Register this middleware in the application's global HTTP middleware stack (app/Http/Kernel.php) | |
protected $middleware = [ | |
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, | |
\App\Http\Middleware\EncryptCookies::class, | |
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, | |
\Illuminate\Session\Middleware\StartSession::class, | |
\Illuminate\View\Middleware\ShareErrorsFromSession::class, | |
\App\Http\Middleware\VerifyCsrfToken::class, | |
\App\Http\Middleware\BeforeAutoTrimmer::class, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment