[英]Getting Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
I'm sending a PUT request that handles input data and updates a record, but I get the above response. 我正在发送一个处理输入数据并更新记录的PUT请求,但是得到了以上响应。 The problem doesn't seem to be the route, however, because if I do
dd($user)
after the $user = User::whereId($id)->firstOrFail();
但是,问题似乎不是路由,因为如果在
$user = User::whereId($id)->firstOrFail();
之后执行dd($user)
$user = User::whereId($id)->firstOrFail();
line, I get the object returned correctly. 行,我得到正确返回的对象。
Yet, when it comes time to validate it, it throws this error. 但是,当需要进行验证时,会引发此错误。
# routes
Route::resource('users', 'UsersController', ['only' => ['index', 'show', 'update']]);
# api call
PUT /users/2
# controller
public function update($id)
{
$user = User::whereId($id)->firstOrFail();
$input = Input::all();
$this->userForm->validate($input);
$user->fill($input)->save();
return $user->toJson();
}
# userForm class
<?php namespace Olp\Forms;
use Laracasts\Validation\FormValidator;
class UserForm extends FormValidator {
protected $rules = [
'email' => 'required|email|unique:users',
'password' => 'required',
'password_confirmation' => 'required|same:password',
'firstname' => 'required',
'lastname' => 'required',
'schoolname' => 'required',
'address1' => 'required',
'address2' => 'required',
'postcode' => 'required',
'city' => 'required'
];
}
and in my UserController: 在我的UserController中:
use Olp\Forms\UserForm;
class UsersController extends \BaseController {
function __construct(UserForm $userForm)
{
$this->userForm = $userForm;
}
I'm not a Laravel guy, but a quick check on their documentation indicates that Resource Controllers support PUT in addition to other HTTP verbs. 我不是Laravel专家,但是快速浏览一下他们的文档表明, 资源控制器除了支持其他HTTP动词外,还支持PUT。
I was not able to figure out how to add HTTP verb support to an arbitrary action, but this indicates that you can name a controller action prefixed with the verb it responds to 我无法弄清楚如何HTTP动词支持添加到任意的动作,但是这表明你能说出与动词前缀的控制器操作它响应
public function putUpdate($id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.