【问题标题】:Laravel Routing User update MethodNotAllowedHttpExceptionLaravel 路由用户更新 MethodNotAllowedHttpException
【发布时间】:2017-11-14 01:26:17
【问题描述】:

我使用的是 Laravel 5.4,但我的 UsersController 中的更新功能存在一些问题。 我总是得到一个“MethodNotAllowedHttpException”。我已经发现它可能会发生,例如当您的表格是发布,但路由得到 - 我猜这里不应该是这种情况......

我只是给你我的代码 sn-ps,也许你已经在那里看到了。只是让你知道:我对 Laravel 完全陌生!

users.blade.php

@extends('layouts.app')

@section('content')


{!! Form::model($user, ['method' => 'PATCH', 'route' => ['users.update',    $user->id]]) !!}

<div class="form-group">
{!! Form::label('email', 'E-Mail:', ['class' => 'control-label']) !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
{!! Form::label('old_password', 'Old assword:', ['class' => 'control- label']) !!}
{!! Form::password('old_password',['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'New password:', ['class' => 'control-label']) !!}
{!! Form::password( 'password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Repeat new password:', ['class' => 'control-label']) !!}
{!! Form::password( 'password_confirmation', ['class' => 'form-control']) !!}
</div>

{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}

{!! Form::close() !!}

{!! Form::model($user, ['method' => 'DELETE', 'route' => ['users.destroy',     $user->id]]) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

@stop

通过 web.php 路由

Route::resource('users', 'UsersController');

在 UsersController.php 中更新

public function update(Request $request, $id)
{
// $user = Auth::user();
$user = User::findOrFail($id);

$this->validate($request, [
  'email' => 'required|string|email|max:255|unique:users',
  'old_password' => 'nullable|string|min:6|confirmed',
  'password' => 'nullable|string|min:6|confirmed',
  'password_comfirmation' => 'nullable|string|min:6|confirmed',
]);

$old_password = $request->old_password;
$password = $request->password;
$password_confirmation->$request->password_confirmation;


if (Hash::check($old_password, $hashedPassword)) {
  if($password == $password_confirmation){

    $user->email = $request->email;
    $user->password = Hash::make($password);
    $user->update();


  }
  else {
    Session::flash('flash_message', 'Password Confirmation not equal!');
    return redirect()->back();
  }
}
else {
  Session::flash('flash_message', 'Old password wrong!');
  return redirect()->back();
}


Session::flash('flash_message', 'User successfully added!');

return redirect()->back();
}

当我执行 php artisan route:list 时,它会告诉我:

    | GET|HEAD  | users/create           | users.create     |   App\Http\Controllers\UsersController@create                            | web          |
    | PUT|PATCH | users/{user}           | users.update     | App\Http\Controllers\UsersController@update                            | web          |
    | GET|HEAD  | users/{user}           | users.show       | App\Http\Controllers\UsersController@show                              | web          |
    | DELETE    | users/{user}           | users.destroy    | App\Http\Controllers\UsersController@destroy                           | web          |
    | GET|HEAD  | users/{user}/edit      | users.edit       | App\Http\Controllers\UsersController@edit  

我还没有真正尝试更新功能,因为我什至没有达到那一点......你有什么提示吗?

----编辑----

这是错误: It looks as if it doesn't allow POST - but I don't even use POST. I tried the POST option in the comments as well, but I still get the same error...

我有一个用于用户的销毁函数,它的工作原理完全相同(但方法:DELETE),它不会抛出任何异常,一切正常。

非常感谢! :) 纳丁

【问题讨论】:

  • 问题可能是由于使用了PATCH。看看这个:stackoverflow.com/questions/25857650/…
  • 你能粘贴你的整个路线文件吗,如果我更新我这样做:Route::post('update', ['as' =&gt; 'update', 'uses' =&gt; 'DanceController@update']);
  • 在{!! Form标签中添加{!! method_field('patch') !!}而不是方法。
  • 我尝试了你所有的想法,但不幸的是我仍然得到同样的错误......我添加了一张带有我得到的错误的图片,也许你有更多的想法,当你看到它时! :)

标签: php laravel exception routing formbuilder


【解决方案1】:

使用 PUT 方法代替 PATCH

{!! Form::model($user, ['method' => 'PUT', 'route' => ['users.update',    $user->id]]) !!}

【讨论】:

  • 对我不起作用:/我在帖子中添加了一张图片,您可能还有其他想法吗?
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 2018-11-25
  • 2019-07-27
  • 2017-01-27
  • 2021-01-25
  • 2016-09-18
  • 1970-01-01
  • 2019-05-15
相关资源
最近更新 更多