【问题标题】:Laravel 5.1: Unable to get form data in middlewareLaravel 5.1:无法在中间件中获取表单数据
【发布时间】:2015-12-09 23:44:36
【问题描述】:

我有一个提交到indexController@postSwitch 的表单提交。我在postSwitch 操作中获得了提交的数据,但是当我在中间件中执行相同的操作时,它返回 null。这是一个全局中间件,我只是想看看我是否可以访问提交的数据。这个documentation 表明我应该能够获取表单数据。

像这样..

// Form: just a simple form that posts `id` to /switch     

// Complete routes
Route::group(['middleware' => ['auth'], function() {
    Route::get('/', 'IndexController@dashboard');
    Route::post('/switch', 'IndexController@postSwitch');
    Route::get('/settings', 'IndexController@settings');
});

Route::get('auth/login',  'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
Route::get('auth/register',  'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');


// Middleware
public function handle($request, Closure $next)
{
    $id = $request->input('id');   
    dd($id);  // null
    return $next($request);
}

// IndexController@switch
public function postSwitchBrand(Request $request)
{
    $id = $request->input('id');  // Has the submitted data
}    

这个中间件是全局注册的。即附加到\App\Http\Kernel 中的$middleware 属性。我错过了什么?

【问题讨论】:

  • 这是您唯一的路线吗?我怀疑,由于您有一个全局路由,它将为每个 HTTP 调用执行。那么,您确定您看到的是正确的日志吗?
  • 添加了所有的路线。我不太明白。我希望这个中间件用于每个请求。
  • 我的意思是说,你的表格POSTswitch 上。但是,由于您有一个全局中间件,您的中间件代码将为所有路由执行。这也包括 GET 路由,它基本上不会向服务器发布任何数据,因此使 id 为 null
  • 我有一个类似的设置,经过测试,我收到了我在中间件中发布的所有数据。我认为我的上述评论指出了确切的问题。
  • 即使中间件用于所有路由/请求(我希望它是),不应该是特定的postswitch 使提交的数据可从请求中获得,@987654333 @?我在路由中尝试了['middleware' => ['auth', 'switch'] 而不是全局,但仍然为空。

标签: php forms laravel-5.1 middleware


【解决方案1】:

根据我们在 cmets 中的讨论,我看到这里有一个小故障,文档中没有定义。我在laravel Git repository 上提出了一个问题,说明了那里的问题。

这里是问题的链接:https://github.com/laravel/framework/issues/11278

laravel 论坛上的链接 :: http://laravel.io/forum/12-11-2015-form-data-not-accessible-in-middleware-via-request

【讨论】:

    【解决方案2】:

    尝试使用以下:

        $request->id;
    

    访问请求参数。

    【讨论】:

    • 这不会给出表单数据。
    【解决方案3】:

    这很棘手,但看起来像以下工作:

    $request->getContent()

    或者如果获取请求的 json 有效负载:

    $request->json()

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2016-04-14
      • 1970-01-01
      • 2016-03-09
      • 2016-01-31
      • 2015-11-26
      • 2020-08-11
      • 2016-03-07
      • 2016-02-02
      相关资源
      最近更新 更多