【发布时间】:2021-06-05 06:33:42
【问题描述】:
我正在开发一个需要用户注册和登录的 Laravel 8 application。
有一个用户个人资料页面,经过身份验证的用户可以编辑他/她自己的数据。
在我的路由文件中:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\HomepageController;
use App\Http\Controllers\Dashboard\DashboardController;
use App\Http\Controllers\Dashboard\UserProfileController;
Route::get('/', [HomepageController::class, 'index'])->name('homepage');
Auth::routes();
Route::group(['middleware' => ['auth']], function() {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/dashboard/profile', [UserProfileController::class, 'index'])->name('profile');
Route::post('/dashboard/profile/update', [UserProfileController::class, 'update'])->name('profile.update');
Route::post('/dashboard/profile/deleteavatar/{id}/{fileName}', [UserProfileController::class, 'deleteavatar'])->name('profile.deleteavatar');
});
问题
我在路由/dashboard/profile/update上需要的方法确实是POST。
不过,每当我在浏览器的地址栏中转到 /dashboard/profile/update 时,我都会收到错误消息:
The GET method is not supported for this route. Supported methods: POST.
我希望我没有收到此错误。我在网上找到的解决方案包括改变方法和其他不令人满意的方法。
使用Route::any 会在更新表单中填写验证错误。
当(并且仅如果)在/dashboard/profile/update 上执行 GET 方法 ID 时,我可以重定向到 /dashboard/profile/ 吗?
最简单的方法是什么?
这样做有什么更好的选择?
【问题讨论】:
-
可能是你的路由文件被缓存了,试试
php artisan route:clear? -
@Espresso 我已经这样做了。
-
Route::any method ?
-
@JohnLobo 使用
Route::any会在更新表单中填写验证错误。 -
根据我的理解,任何路由都应该允许所有方法。你也可以显示表单和控制器代码