Web Niraj
  • Facebook
  • Flickr
  • Github
  • 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:

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:

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 ReplyCancel 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

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon 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 Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy