laravel - 此路由不支持 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。
不知道是什么导致了错误,感谢帮助。
解决方案
如果我理解正确,这是用于存储功能吗?那么您不必将其放入@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 >
我希望它有帮助!
推荐阅读
- excel - VBA:获取“编译错误 - 无效使用属性”
- c# - 如何将事件从控制台发送到窗口窗体C#
- javascript - 在CKEditor 5中动态调整粘性工具栏的位置
- php - Laravel 创建 PDF 语法错误,意外 '->' (T_OBJECT_OPERATOR)
- elasticsearch - 基于字段的 Elasticsearch 订单聚合桶(可以是文本/字符串)
- youtube - YouTube“书呆子统计”的编解码器部分中的数字是什么意思
- sql - 使用三个表的 SQL SELECT 语句
- azure-devops - Azure devops - 对 drop 中的目录集合执行相同的操作
- rest - 可在 Internet 上访问的微服务端点
- sql - 如何在 VB.net 中创建 SQL 数据库?