使用POST方法提交表单以及如何获取url中的参数

问题描述 投票:0回答:1

尝试使用POST方法提交表单并通过url获取参数。访问 website.com/profile/ 时它不起作用。它返回此错误消息

路由配置文件列表不支持 GET 方法。支持的 方法:POST。

我不知道我做错了什么

Web.php

Route::get('/profile/{username}/{mobilenumber?}', [ProfileController::class, 'profileList'])->name('profile-list');
Route::post('/profile/', [ProfileController::class, 'submitForm'])->name('submit-form');

刀片形状文件

<form action="{{ route('submit-form') }}" method="post">
   @csrf
   ....
</form>

配置文件控制器

public function submitForm(Request $request) 
    {
        $request->validate(
            ...
        );

        $username = strtolower($request->input('username'));
        $mobilenumber = strtolower($request->input('mobilenumber'));

        if ($mobilenumber) {
            return redirect()->route('profile-list', ['username' => $username, 'mobilenumber' => $mobilenumber]);
        } else {
            return redirect()->route('profile-list', ['username' => $username]);
        }
    }
php laravel routes
1个回答
0
投票

尝试像这样更改你的路线定义

Route::post('/profile', 'ProfileController@submitForm')->name('submit-form');

并添加 => @method('POST')

<form action="/profile" method="post">
    @method('POST')
    @csrf
</form>
© www.soinside.com 2019 - 2024. All rights reserved.