Web Niraj
  • Facebook
  • Flickr
  • Github
  • Instagram
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Laravel 5.2: Trim All Input using Middleware

Laravel 5.2: Trim All Input using Middleware

0

Laravel 5.2 is already good at sanitizing input data received from GET and POST data, but it doesn’t remove excessive spacing at the beginning or end of the data. This is in particular useful since some mobile devices often leave a trailing space thanks to auto-correct. Luckily, this is easy to achieve using Custom Middleware.

Create Middleware

You can using the artisan make command to help in creating the middleware file we will be using to trim all input. This can be achieved by running the command (where InputTrim is the name of the middleware):

php artisan make:middleware InputTrim

Trimming the Input

In the newly created middleware file (located at app/Http/Middleware/InputTrim.php), you just need to add one line of code above return $next($request) – see Line 18 of the below code:

<?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);
}
}
view raw InputTrim.php hosted with ❤ by GitHub

Updating the Laravel Kernel

Once you have updated the middleware, you need to tell Laravel to run the middleware on every request (or for a particular group). To do this, you need to update the app/Http/Kernel.php file.

To run the Middleware on all HTTP requests, add line 11 from the below code  to the end of the $middleware array:

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
// ..
\App\Http\Middleware\InputTrim::class,
];
protected $middlewareGroups = [
// ...
];
protected $routeMiddleware = [
// ...
];
}
view raw Kernel.php hosted with ❤ by GitHub

Your Kernel.php should look like the above example. In our version, we have removed other middleware and group settings.

Credit to @t202wes for a version that works with arrays.

Input Sanitization, Laravel 5, Laravel 5.2, PHP

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Subscribe for updates

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Flat File Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux Mac OS MongoDB NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins

© 2011-2026 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy