Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IlyaZha/a72ea944f1fb7daebbee9bbcefb97399 to your computer and use it in GitHub Desktop.
Save IlyaZha/a72ea944f1fb7daebbee9bbcefb97399 to your computer and use it in GitHub Desktop.
Auto trim all input [Laravel 5]
// 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