Search code examples
laravellaravel-5.3laravel-5.4

How can i add TrimString Middleware in laravel 5.3?


Just came to know that Laravel 5.4 has an awesome feature TrimString, which removes the white spaces from any input. I want this middleware in my 5.3 project, any idea how to do that?

I just copied the middleware from GitHub repo of Laravel but it is not working.

Thanks


Solution

  • If you want to use this feature in Laravel 5.3.

    Add these two classes into your App\Http\Middleware

    https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php

    https://github.com/laravel/laravel/blob/master/app/Http/Middleware/TrimStrings.php

    Updating it's namespace to App\Http\middleware. Like:

    TransformsRequest.php

    namespace App\Http\Middleware;
    
    use Closure;
    use Symfony\Component\HttpFoundation\ParameterBag;
    
    class TransformsRequest
    {
        /**
         * The additional attributes passed to the middleware.
         *
         * @var array
         */
        protected $attributes = [];
    
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next, ...$attributes)
        {
            $this->attributes = $attributes;
    
            $this->clean($request);
    
            return $next($request);
        }
    
        /**
         * Clean the request's data.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return void
         */
        protected function clean($request)
        {
            $this->cleanParameterBag($request->query);
    
            $this->cleanParameterBag($request->request);
    
            if ($request->isJson()) {
                $this->cleanParameterBag($request->json());
            }
        }
    
        /**
         * Clean the data in the parameter bag.
         *
         * @param  \Symfony\Component\HttpFoundation\ParameterBag  $bag
         * @return void
         */
        protected function cleanParameterBag(ParameterBag $bag)
        {
            $bag->replace($this->cleanArray($bag->all()));
        }
    
        /**
         * Clean the data in the given array.
         *
         * @param  array  $data
         * @return array
         */
        protected function cleanArray(array $data)
        {
            return collect($data)->map(function ($value, $key) {
                return $this->cleanValue($key, $value);
            })->all();
        }
    
        /**
         * Clean the given value.
         *
         * @param  string  $key
         * @param  mixed  $value
         * @return mixed
         */
        protected function cleanValue($key, $value)
        {
            if (is_array($value)) {
                return $this->cleanArray($value);
            }
    
            return $this->transform($key, $value);
        }
    
        /**
         * Transform the given value.
         *
         * @param  string  $key
         * @param  mixed  $value
         * @return mixed
         */
        protected function transform($key, $value)
        {
            return $value;
        }
    }
    

    TrimStrings.php

    namespace App\Http\Middleware;
    
    class TrimStrings extends TransformsRequest
    {
        /**
         * The attributes that should not be trimmed.
         *
         * @var array
         */
        protected $except = [
            //
        ];
    
        /**
         * Transform the given value.
         *
         * @param  string  $key
         * @param  mixed  $value
         * @return mixed
         */
        protected function transform($key, $value)
        {
            if (in_array($key, $this->except)) {
                return $value;
            }
    
            return is_string($value) ? trim($value) : $value;
        }
    }
    

    And add into your App\Http\Kernel.php

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\TransformsRequest::class,
        \App\Http\Middleware\TrimStrings::class,
    ];
    

    To use it just use:

    dd(request('email'));
    

    More on it:

    https://laravel-news.com/laravel-5-4-middleware