【发布时间】:2022-01-07 07:02:57
【问题描述】:
我正在尝试使用 Put 方法,这是我的结果....
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
http://localhost:3000/admin/genres/
这是路由:
Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::redirect('/', 'records');
Route::resource('genres', 'Admin\GenreController');
Route::get('records', 'Admin\RecordController@index');
});
这是控制器中的函数:
public function update(Request $request, Genres $genres)
{
// Validate $request
$this->validate($request,[
'name' => 'required|min:3|unique:genres,name,' . $genres->id
]);
// Update genre
$genres->name = $request->name;
$genres->save();
// Flash a success message to the session
session()->flash('success', 'The genre has been updated');
// Redirect to the master page
return redirect('admin/genres');
}
这是刀片中的 HTML 表单代码:
@extends('layouts.template')
@section('title', 'Edit genre')
@section('main')
<h1>Edit genre: {{ $genres->name }}</h1>
<form action="/admin/genres/{{ $genres->id }}" method="post">
<!-- this is where is goes wrong i guess... -->
@method('put')
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name"
class="form-control @error('name') is-invalid @enderror"
placeholder="Name"
minlength="3"
required
value="{{ old('name', $genres->name ?? '') }}">
@error('name')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-success">Save genre</button>
</form>
@endsection
这是路由表:
+--------+----------------------------------------+---------------------------+------------------+------------------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+---------------------------+------------------+------------------------------------------------------------------------+------------+
| | GET|HEAD | / | | Illuminate\Routing\ViewController | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | admin | | Illuminate\Routing\RedirectController | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres | genres.index | App\Http\Controllers\Admin\GenreController@index | web |
| | | | | | auth |
| | | | | | admin |
| | POST | admin/genres | genres.store | App\Http\Controllers\Admin\GenreController@store | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/create | genres.create | App\Http\Controllers\Admin\GenreController@create | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/{genre} | genres.show | App\Http\Controllers\Admin\GenreController@show | web |
| | | | | | auth |
| | | | | | admin |
| | PUT|PATCH | admin/genres/{genre} | genres.update | App\Http\Controllers\Admin\GenreController@update | web |
| | | | | | auth |
| | | | | | admin |
| | DELETE | admin/genres/{genre} | genres.destroy | App\Http\Controllers\Admin\GenreController@destroy | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/{genre}/edit | genres.edit | App\Http\Controllers\Admin\GenreController@edit | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/records | | App\Http\Controllers\Admin\RecordController@index | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | contact-us | | App\Http\Controllers\ContactUsController@show | web |
| | POST | contact-us | | App\Http\Controllers\ContactUsController@sendEmail | web |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web |
| | | | | | auth |
| | GET|HEAD | itunes | | App\Http\Controllers\ItunesController@index | web |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web |
| | | | | | guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web |
| | | | | | guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | password/confirm | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm | web |
| | | | | | auth |
| | POST | password/confirm | | App\Http\Controllers\Auth\ConfirmPasswordController@confirm | web |
| | | | | | auth |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web |
| | | | | | guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web |
| | | | | | guest |
| | GET|HEAD | shop | | App\Http\Controllers\ShopController@index | web |
| | GET|HEAD | shop/{id} | | App\Http\Controllers\ShopController@show | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | user | | Illuminate\Routing\RedirectController | web |
| | | | | | auth |
| | GET|HEAD | user/password | | App\Http\Controllers\User\PasswordController@edit | web |
| | | | | | auth |
| | POST | user/password | | App\Http\Controllers\User\PasswordController@update | web |
| | | | | | auth |
| | GET|HEAD | user/profile | | App\Http\Controllers\User\ProfileController@edit | web |
| | | | | | auth |
| | POST | user/profile | | App\Http\Controllers\User\ProfileController@update | web |
| | | | | | auth |
+--------+----------------------------------------+---------------------------+------------------+------------------------------------------------------------------------+------------+
我真的很想得到一些帮助,因为这是我的学校项目:D 我的朋友项目有效,但我们不知道为什么它不起作用。
对不起,我的英语不好:D
【问题讨论】:
-
您的路线指定了“get”方法:
Route::get('records', 'Admin\RecordController@index');。将其更改为Route::put -
那是一个不同的页面正在使用的那个 ````Route::resource('genres', 'Admin\GenreController');```
-
执行
php artisan routes:list以查看资源定义了哪些路由 -
路由表已添加:D
-
您的表单操作是
/admin/genres/{{ $genres->id }},但错误消息显示您正在点击/admin/genres/。看起来$genres->id可能会丢失?查看您的 html 表单上的源代码并查看该表单操作 URL 的样子。
标签: php laravel-8 laravel-blade