【问题标题】:The PUT method is not supported for this route此路由不支持 PUT 方法
【发布时间】:2019-11-14 16:13:37
【问题描述】:

我无法通过 laravel 控制器中的存储功能提交表单信息。表单需要为注册用户创建一个新的配置文件。

我什至重新创建了项目,并重做了表单 - 回到纯 html,因为我怀疑 laravelCollective 函数可能会导致它,但仍然是相同的错误。 我什至按照另一个帖子/线程中的建议重新排列了表单属性。

我什至重新创建了项目,并重做了表单 - 回到纯 html,因为我怀疑 laravelCollective 函数可能会导致它,但仍然是相同的错误。 我什至按照另一个帖子/线程中的建议重新排列了表单属性。

形式:

< form method="POST" enctype="multipart/form-data" action="{{ url('users/profile') }}" accept-charset="UTF-8" >
    @csrf
...
// input fields here
...
< /form >

路线:

    Route::resource('users/profile', 'ProfileController');

    Route::get('/home', 'HomeController@index')->name('home');

    Route::resource('users', 'UserController');
    Route::post('users/profile', 'ProfileController@store')->name('profile.store');

ProfileController@store 函数: //省略部分代码

    public function store(Request $request)
    {

        $this->validate($request, [
            'firstname'=>'required',
            'lastname'=>'required', 
            ...
            'desc'=>'required'
        ]);


        //handle file upload
        if($request->hasFile('cover_image')) {
            //Get file name with extension
            $fileNameWithExt = $request->file('cover_image')->getClientOriginalName();
            //Just file name

            $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);

            //Just Ext
            $ext = $request->file('cover_image')->getClientOriginalExtension();

            //FileName to Store
            $fileNameToStore = $fileName.'_'.time().'_'.$ext;
            //upload image 
            $path = $request->file('cover_image')->storeAs('public/users/'.auth()->user()->id.'cover_images/'.$request->input('firstname').'_'.$request->input('lastname').'_'.auth()->user()->id.'/',$fileNameToStore);


        } else {
            $fileNameToStore = 'noimage.jpg';
        }
        /*
        */

        $profile = new Profile;
        $profile->firstname = $request->input('firstname');
        $profile->lastname = $request->input('lastname');
        ...
        $profile->desc = $request->input('desc');
        $profile->save();
        return redirect('/users/profile');//->with('success','Profile Created');
    }

著名的错误:

Symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 不支持 PUT 方法 路线。支持的方法:GET、HEAD、POST。

不确定是什么导致了错误,感谢帮助。

【问题讨论】:

    标签: laravel forms laravel-5


    【解决方案1】:

    如果我理解正确,这是为了存储功能,对吗?那么您不必将@method('PUT') 放入您应该发布的表单中。在资源中存储的路径是POST。

    这是我删除@method('PUT')的代码

    < form method="POST" enctype="multipart/form-data" action="{{ url('users/profile') }}" accept-charset="UTF-8" > 
    @csrf ... 
    // input fields here ... 
    < /form >
    
    The Routes: Route::resource('users/profile', 'ProfileController');
    
    Route::get('/home', 'HomeController@index')->name('home');
    
    Route::resource('users', 'UserController'); Route::post('users/profile', 'ProfileController@store')->name('profile.store');
    

    PUT 方法用于更新。在控制器中更新时,您需要在表单中传递 id,应该如下所示。

    < form method="POST" enctype="multipart/form-data" action="{{ url('users/profile', $data->id) }}" accept-charset="UTF-8" >
    @method('PUT') 
    @csrf ... 
    // input fields here ... 
    < /form >
    

    希望对你有帮助!

    【讨论】:

    • 还是不行。即使我只是尝试 d​​d() 内容。
    【解决方案2】:

    您的路线文件有问题,只需将您的编辑路线更改为该路线

    Route::match(['put', 'patch'], 'the path you want /{id}','controllername@functionname');
    

    您应该注意到,如果您是 laravel 新手,您应该将 id 传递给此路由,如本部分 {id} 所示,以便您的编辑功能可以显示它以前的数据以及如果你想提交一个表单,它应该有 put 方法,而 html 基本表单不支持,所以你应该找到一种方法来提交它,比如使用 laravel 集体 或者放一个 表单中的隐藏方法 如果还不行请给我打电话

    【讨论】:

      【解决方案3】:

      当在路由上使用 Laravel 资源方法时,它可以让事情在它所期望的方面变得非常具体。如果您查看manual, 上的图表,它正在寻找一个 uri,其中更新元素 id 作为 uri 的一部分返回。该示例如下所示:/photos/{photo}。我不确定这就是您构建 html 表单的方式。

      你说你正在使用 LaravelCollective 来完成这项工作。这通常工作得很好,并且具有简单的表单模型绑定的巨大优势。但它有助于包含命名路由,其中​​包括更新资源的“更新”。例如:

      {!! Form::model($yourModel, array('route' => array('yourRoute.update', 
                        $yourModel->id), 'method'=>'PUT', 'id'=>'Form'))!!}
      

      我对使用这种方法的集体没有问题。

      【讨论】:

        猜你喜欢
        • 2020-04-13
        • 2021-11-30
        • 1970-01-01
        • 2021-09-12
        • 2019-12-29
        • 2020-04-03
        • 2020-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多