Laravel 5.4从请求对象重定向获取方法 [英] Laravel 5.4 redirect taking method from request object

查看:241
本文介绍了Laravel 5.4从请求对象重定向获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始了一个Laravel 5.4项目,发现了一些我不太了解的东西,我发布了这个问题,希望能解决此问题并找出它是预期的行为还是错误.

Recently I have started a Laravel 5.4 project and I found something I don't quite understand and I'm posting this question hoping to solve this issue and find out if it's expected behaviour or a bug.

问题是,我正在提交一个如下所示的更新表单:

The thing is, I'm submitting an update form which looks like so:

<form id="post-form" action="{{url('admin/posts/'.$post->id)}}" method="POST">
  {{ csrf_field() }}
  {{ method_field('PUT') }}
  <!-- Some fields -->
</form>

然后我尝试在控制器方法上验证该请求:

And I try to validate that request on the controller method:

$validator = Validator::make($request->all(), [
        // Fields to validate
    ]);
if ($validator->fails()) {
   return back()
   ->withErrors($validator)
   ->withInput();
}

我的问题出在验证失败并执行重定向时,因为我收到代码 405 错误MethodNotAllowedHttpException.

My problem comes when the validation fails and the redirect is performed, because I get a code 405 error MethodNotAllowedHttpException.

实际上,URL是正确的,但是方法是错误的,如HTTP标头所示:

Indeed, the url is correct but the method is wrong, as shown in the HTTP headers:

Request URL:https://myproject.dev/admin/posts/1/edit
Request Method:PUT
Status Code:405 
Remote Address:127.0.0.1:443
Referrer Policy:no-referrer-when-downgrade

应该是GET而不是PUT.我最好的猜测是使用$request对象的_method属性进行重定向,而不是使用GET方法.

It should be a GET instead of a PUT. My best guess is that is using the _method property of the $request object to redirect, instead of using the GET method.

在重定向调用之前的dump($request->all()):

array:19 [
  "_token" => "XOtwEeXwgnuc8fNqCwxkdO992bU6FObDwTuCg1cJ"
  "_method" => "PUT"
  //fields
]

有些运气我已经尝试过了:

Some things I have already tried with no luck:

  • 从请求中取消_method:unset($request['_method'])
  • 命名路由并根据该名称进行重定向:

  • Unsetting the _method from the request: unset($request['_method'])
  • Name the route and redirect based in that name:

// The routes file (routes/web.php)
Route::get('posts/{id}/edit', 'PostsController@edit')->name('edit-post');

// The redirect (controller)
return redirect()->route('edit-post', ['id' => 1])
    ->withErrors($validator)
    ->withInput();

  • 在重定向中指定网址

  • Specify the url in the redirect

    // The redirect (controller)
    return redirect('admin/posts/'.$post->id.'/edit')
        ->withErrors($validator)
        ->withInput();
    

  • 那么我该如何使用普通的GET方法执行该重定向?这是预期的行为吗?预先谢谢您,任何建议或提示都将不胜感激.

    So how could I perform that redirect with a normal GET method? Is this an expected behaviour? Thank you in advance, any advice or hint is much appreciated.

    推荐答案

    您需要在routes.php文件中使用一条单独的放置路径.

    You need a separate put route in your routes.php file.

    Route::get('posts/{id}/edit', 'PostsController@edit')->name('edit-post');
    Route::put('posts/{id}/edit', 'PostsController@edit')->name('edit-post');
    

    get函数意味着此路由将使用GET方法处理路由.您需要添加put来使用PUT方法处理请求. 文档中提供了所有可用的路由方法.

    The get function means this route will handle routes using the GET method. You need to add a put to handle requests using the PUT method. All available route methods are available in the documentation.

    您还可以使用match函数在一行中匹配多个方法.像这样:

    You can also use the match function to match multiple methods in one line. Like so:

    Route::match(['get', 'put'], 'posts/{id}/edit', 'PostsController@edit');
    

    这篇关于Laravel 5.4从请求对象重定向获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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