首页 > 解决方案 > 此路由不支持 PUT 方法

问题描述

我无法提交表单信息以在 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。

不知道是什么导致了错误,感谢帮助。

标签: laravelformslaravel-5

解决方案


如果我理解正确,这是用于存储功能吗?那么您不必将其放入@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 >

我希望它有帮助!


推荐阅读