php MethodNotAllowedHttpException laravel-4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17501653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:01:36  来源:igfitidea点击:

MethodNotAllowedHttpException laravel-4

phpformsexceptionlaravellaravel-4

提问by bigData

Form :

形式 :

{{ Form::open(array('url' => 'user/create', 'files' => true)) }}

Route :

路线 :

Route::resource('user', 'UserController');

UserController.php

用户控制器.php

  class UserController extends BaseController {

    public function index()
    {
        return 'hi11';
        //return View::make('home.index');
    }
    public function create()
    {
        return 'hi22';
        //return View::make('home.index');
    }

}

This code gives
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

这段代码给出
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

回答by Barnabas Kendall

I'd just like to add my own discovery along these lines... Maybe this will save someone else the head-scratching I just performed.

我只想沿着这些方向添加我自己的发现......也许这会为其他人省去我刚刚表演的头疼。

I too implemented the Route::resource mechanism. I couldn't figure out why my create was working but my update was not. It turns out you can't reuse the same form code exactly, the form that does an update must use the method PUT or PATCH. Why update couldn't be a POST is beyond me.

我也实现了 Route::resource 机制。我不知道为什么我的创建有效,但我的更新无效。事实证明,您不能完全重用相同的表单代码,进行更新的表单必须使用 PUT 或 PATCH 方法。为什么更新不能是 POST 超出了我的范围。

That is to say, the opening form tag for an update must look like this:

也就是说,更新的打开表单标签必须是这样的:

Form::model($thing, array(
    'method' => 'PUT', 
    'route' => array('things.update', $thing->id)
    )

Without specifying method => PUT, you get this not-helpful error.

如果不指定方法 => PUT,您会得到这个无用的错误。

回答by pgk

Because in your roures you use resourse controller, you can use only specific paths and actions, described in documentation http://laravel.com/docs/controllers#resource-controllers.

因为在您的 roures 中您使用资源控制器,所以您只能使用特定的路径和操作,如文档http://laravel.com/docs/controllers#resource-controllers 中所述

user/create ( UserController::create ) is where you need to show the form for adding a new user.

user/create ( UserController::create ) 是您需要显示用于添加新用户的表单的地方。

The actual storage of the user should be done in user/store i.e. your form must be send data to UserController::store() method.

用户的实际存储应该在 user/store 中完成,即您的表单必须将数据发送到 UserController::store() 方法。

In your case if you POST your form only to 'url' => 'user', this should automatically send data to the correct method.

在您的情况下,如果您仅将表单发布到 'url' => 'user',这应该会自动将数据发送到正确的方法。

回答by Laurence

Laravel 4 resources have named routes- just use those:

Laravel 4 资源已命名路由- 只需使用这些:

{{ Form::open(array('route' => 'user.create', 'files' => true)) }}

回答by Angel M.

this is how I'm doing it, it might help someone, can be improved, but this would be main idea.

这就是我这样做的方式,它可能对某人有所帮助,可以改进,但这将是主要思想。

@if(isset($data))
    {{ Form::open(['route'=>['blog.update', isset($data) ? $data->slug : null],'method' => 'patch','role' => 'form', 'class' => 'blog-form form-horizontal']) }}
@else
    {{ Form::open(['route'=>'blog.store','role' => 'form', 'class' => 'blog-form form-horizontal']) }}
@endif