如何获取 Laravel 中带空格的请求?

How to get a request with spaces in Laravel?

向服务器发送数据:

'   test_data   '

Request $request来:

'test_data   '

如何获取带空格的数据(不要 trim 它们)?

有两种方法:

  1. 通过从 Kernel class:
  2. 注释掉它来完全禁用 TrimStrings 中间件
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        // \App\Http\Middleware\TrimStrings::class, <-- This is the middleware
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

// [...]
  1. 如果你只是需要避免特定输入的trim,你必须更新TrimStrings中间件:
class TrimStrings extends Middleware
{
    /**
     * The names of the attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        'password',
        'password_confirmation',
        'first_input',
        'another_input', // Here add your inputs
        'my_other_input'
    ];

// [...]