如何在Laravel 5.3中添加TrimString中间件? [英] How can i add TrimString Middleware in laravel 5.3?

查看:439
本文介绍了如何在Laravel 5.3中添加TrimString中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚知道Laravel 5.4有一个很棒的功能 TrimString ,该功能可以从任何输入中删除空格.我想在我的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?

我刚刚从Laravel的GitHub存储库中复制了中间件,但是它不起作用.

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

谢谢

推荐答案

如果要在Laravel 5.3中使用此功能.

If you want to use this feature in Laravel 5.3.

将这两个类添加到您的App\Http\Middleware

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

namespace更新为App\Http\middleware. 喜欢:

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;
    }
}

并添加到您的App\Http\Kernel.php

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

要使用它,只需使用:

dd(request('email'));

更多信息:

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

这篇关于如何在Laravel 5.3中添加TrimString中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆