Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Created May 7, 2015 13:51
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save drakakisgeo/3bba2a2600b4c554f836 to your computer and use it in GitHub Desktop.
Save drakakisgeo/3bba2a2600b4c554f836 to your computer and use it in GitHub Desktop.
Auto trim all input [Laravel 5]
// Step 1. Create this class in the middleware folder.
<?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)
{
$request->merge(array_map('trim', $request->all()));
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',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
'App\Http\Middleware\BeforeAutoTrimmer'
];
@kjdion84
Copy link

kjdion84 commented Jun 4, 2017

Here is how I use it in a static helper class outside Laravel.

	// set trimmed & nulled request
	static function setRequest () {
		self::$request = Request::capture();
		self::$request->merge(array_map(function ($value) {
			if (is_string($value)) {
				if (empty($value)) {
					return null;
				}
				else {
					return trim($value);
				}
			}
			else {
				return $value;
			}
		}, self::$request->all()));
	}

@msbrime
Copy link

msbrime commented Jul 25, 2017

For form submissions I use an extension of Laravel's request class that I put together here .
You can skip some fields by adding them to the untrimmable array, use dot indexing for nested keys

@jartaud
Copy link

jartaud commented Jun 5, 2018

For Lavavel >= 5.4, please check the middlewares TrimStrings and ConvertEmptyStringsToNull.

@Quix0r
Copy link

Quix0r commented Oct 29, 2018

You wish not to use strings for referencing class names. Better use \App\Some\Foo::class instead of "\App\Some\Foo" because typing-mistakes can be quickly found with first way but are hidden away with 2nd method as it is only a string where PHP cannot check if the class reference is still valid.

Example: \Apps\Some\Foo::class <-- see the small s in it? It will be quickly found by PHP.

@Quix0r
Copy link

Quix0r commented Oct 29, 2018

@shojibflamon you properly want to compare type-safe, means $item === "", else maybe (int) 0 will be converted to NULL which is not intended (e.g. you have a database column is_enabled which has tinyint(1) unsigned not null default 1 then you surely want 0 be set!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment